从.host.com重定向到.htaccess中的host.com

时间:2011-05-06 06:08:16

标签: .htaccess mod-rewrite apache2

有人可以在.htaccess文件中向我提供正确的指令,让www.host.com请求重定向到host.com,反之亦然吗?

1 个答案:

答案 0 :(得分:2)

这将从http://www.yoursite.com重定向到http://yoursite.com

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^yoursite\.com
RewriteRule (.*) http://yoursite.com/$1 [R=301, L]

这是相反的:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]

以上是我在服务器上使用的内容,它运行良好。