.htaccess mod_rewrite重定向

时间:2012-02-01 08:16:48

标签: .htaccess mod-rewrite

示例#1(按预期工作):

RewriteCond %{HTTP_HOST} ^www.hanoichurch.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.org$ [OR]
RewriteCond %{HTTP_HOST}     ^hanoiworship.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hanoiworship.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/hanoichurch\.com\/$1" [R=301,L]

样品#2(不起作用*):

RewriteCond %{HTTP_HOST} ^www.nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST}     ^nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nathanjbrauer.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/nathanjbrauer\.com\/$1" [R=301,L]

*默默失败(访问www.nathanbrauer.com只是加载页面而不会重定向到nathanjbrauer.com)。

百万感谢!

===更新===

我最终做的是使用此代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^nathanjbrauer\.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^ http://nathanjbrauer.com%{REQUEST_URI} [R=301,L]

1 个答案:

答案 0 :(得分:2)

您的sample1代码也可以重构,但我只建议对您的示例2代码进行修复,因为这不适合您:

RewriteCond %{HTTP_HOST} ^(www\.)?nathanbrauer\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.nathanjbrauer\.com$
RewriteCond %{REQUEST_FILENAME} !^google [NC]
RewriteRule ^ http://nathanjbrauer.com%{REQUEST_URI} [R=301,L]