Redirect 如何防止服务器重定向http://domain.com/sub 到http://www.domain.com/sub

Redirect 如何防止服务器重定向http://domain.com/sub 到http://www.domain.com/sub,redirect,Redirect,我有一个大问题,我检查了我的域名重定向在这里,然后结果是 "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 Co

我有一个大问题,我检查了我的域名重定向在这里,然后结果是

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


可以这样做吗?

将以下内容放入文档根
.htaccess
文件中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.){0,1}.*[^/]$ [OR]
RewriteRule ^(.*)$ http://domain.com/$1/ [R=301,L]
请注意,
.htaccess
文件中的其他条目可能会干扰此操作,因此您可能需要删除一些其他条目