3

我想强制一个www。通过使用 .htaccess 301 重定向在我的网站上添加前缀。我目前正在尝试:

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

这通常可以工作,但我使用的是 Zend Framework,它会导致所有请求都被重定向回http://www.mysite.com/index.php,而不管初始请求如何。

例如...

http://mysite.com/blog, 
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,

将全部重定向到http://www.mysite.com/index.php

但是,如果我最初请求特定文件,例如...

http://mysite.com/some-file.htm

重定向工作正常,重定向到http://www.mysite.com/some-file.htm

4

2 回答 2

4

第一次,不要忘记启用重写(“RewriteEngine on”)。如果您使用 Zend Framework,最后一行很重要。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php

现在网址...

http://mysite.com/some-file.htm

...重定向到http://www.mysite.com/some-file.htm但使用 index.php

于 2009-03-28T00:43:39.120 回答
1

重定向到 www 时不要忘记忽略子域。

http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/

于 2011-01-16T19:25:18.407 回答