.htaccess从URL +目录中删除WWW

时间:2011-06-29 02:16:11

标签: .htaccess url mod-rewrite no-www

这对许多人来说似乎不是问题(阅读:我找不到答案),但我想更新以下htaccess代码,不仅要从URL中删除'www',还要任何被访问的子目录。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

有了这个,http://www.example.com/any/解析得很好,但我希望它像根一样重定向到http://example.com/any/

6 个答案:

答案 0 :(得分:130)

我遇到了同样的问题(无法从指向附加域上的子目录的URL中剥离'www'),但经过一些试验和错误后,这似乎对我有用:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

答案 1 :(得分:17)

非www =>的重定向代码www和对面www =>非www。 .htaccess文件中没有硬编码域和方案。因此将保留原始域和http / https版本。

APACHE 2.4和NEWER

非WWW => WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]

注意:不在Apache 2.2上工作,其中%{REQUEST_SCHEME}不可用。为了与Apache 2.2兼容,请使用下面的代码或将%{REQUEST_SCHEME}替换为固定的http / https。

APACHE 2.2和NEWER

非WWW => WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

......或更短版本......

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

...短版本无法实现,因为%N只能从最后一个RewriteCond获得...

答案 2 :(得分:12)

我认为你很接近,但请尝试以下方法:

# force non-www domain
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

不确定您对子目录的确切含义,但这符合您的示例。

答案 3 :(得分:1)

我用它并且它对我有用

RewriteCond %{HTTP_HOST} ^www.locphen.vn/$ [NC]
RewriteRule ^(.*)$ http://locphen.vn/$1 [R=301,L]

示例:http://www.locphen.vn/he-thong-loc-nuoc-gieng.html - > http://locphen.vn/he-thong-loc-nuoc-gieng.html

答案 4 :(得分:-1)

I use this code. If my visitor does not have www in his url then this condition adds www with url, otherwise no need to add www with url ( because he already has. :) )

RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www\.YOUR-SITE\.com$ [NC]
RewriteRule ^(.*) http://www.YOUR-SITE.com/$1 [L,R]

答案 5 :(得分:-3)

您好,代码工作正常,除了它在带有值的URL中传递www,并在末尾显示参数和值的斜杠。

< p>

RewriteEngine On SetOutputFilter DEFLATE AddOutputFilterByType DEFLATE text/html text/plain text/xml RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(!.(\.gif|\.jpg|\.png|\.swf|\.css|\.js|\.txt|\.php|\.htm|\.html)|.+[^/])$ /$1/ [R=301,NC] RewriteRule ^(.[^.*]+)\/$ ?jogar=$1 [NC] Options -Indexes Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http:\/\/%1%{REQUEST_URI} [R=301,QSA,NC,L]