强迫非www到htaccess中的www

时间:2012-01-25 08:03:11

标签: apache .htaccess

我有:

# BEGIN WithoutWWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
# END WithoutWWW

但它不起作用。我仍然可以访问www和非www版本。我做错了什么?

2 个答案:

答案 0 :(得分:6)

尝试:


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

//OR
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 1 :(得分:1)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteRule ^(.*)$ [R=301,L]