如果htaccess中没有匹配上述条件,则重定向

时间:2015-02-20 17:07:46

标签: regex .htaccess redirect

我正在将旧域的所有页面重定向到新域的类似页面并且它正常工作但我还想要的是,如果用户试图访问其他不重定向的页面它应该自动重定向到新域的主页。

以下是文件的一部分

RewriteEngine On 
RewriteBase / 
Redirect 301 /about-us/ http://www.newdomain.com/about_us 
Redirect 301 /contact-us/ http://www.newdomain.com/contact 

它工作得很好但如果用户试图访问上面没有提到的任何其他页面,我该如何重定向?我尝试将下面的代码添加到文件的末尾,但它开始将每个url重定向到主页

RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/ [L,R=301]

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则:

RewriteEngine On 

RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC]
RewriteRule ^(about-us|contact-us)/?$ http://www.newdomain.com/$1 [L,NC,R=302] 

RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC]
RewriteRule . http://www.newdomain.com/ [L,R=302]
相关问题