如何防止将http://domain.com/sub重定向到http://www.domain.com/sub

时间:2011-03-03 07:31:20

标签: redirect

我遇到了一个大问题,我在这里检查了我的域名重定向http://www.redirectcheck.com/,结果是

"http://domain.com/sub"

HTTP/1.1 301 Moved Permanently Date: Thu, 03 Mar 2011 07:14:30 GMT Server: Apache/2.2.3 (CentOS) Location: http://www.domain.com/sub/ Vary: Accept-Encoding Content-Length: 326 Connection: close Content-Type: text/html; charset=iso-8859-1

"http://www.domain.com/sub/"

HTTP/1.1 301 Moved Permanently Date: Thu, 03 Mar 2011 07:14:31 GMT Server: Apache/2.2.3 (CentOS) Location: http://domain.com/sub/ Vary: Accept-Encoding Content-Length: 322 Connection: close Content-Type: text/html; charset=iso-8859-1

"http://domain.com/sub/"

HTTP/1.1 200 OK Date: Thu, 03 Mar 2011 07:14:31 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8

我希望它是这样的:

当我转到“http://domain.com/sub”(没有斜杠)时,它应该重定向到“http://domain.com/sub/”

当我转到“http://www.domain.com/sub”(没有斜杠)时,它应该重定向到“http://domain.com/sub/”

有可能吗?

1 个答案:

答案 0 :(得分:0)

将以下内容放入文档根目录.htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.){0,1}.*[^/]$ [OR]
RewriteRule ^(.*)$ http://domain.com/$1/ [R=301,L]

请注意,.htaccess文件中的其他条目可能会对此产生干扰,因此您可能希望删除其他一些条目。