2011-03-03 84 views
0

我有一个大问题,我在此检查了我的域的重定向http://www.redirectcheck.com/,结果是如何防止http://domain.com/sub重定向到http://www.domain.com/sub

"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/“

有没有可能这样做?

回答

0

将以下到您的文档根目录.htaccess文件:

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

要知道,虽然在.htaccess文件,其他项都可能与此干扰,所以你可能需要删除一些其他条目。