mod_rewrite - 排除无效的网址

时间:2015-10-29 13:45:39

标签: apache mod-rewrite url-redirection

这应该是答案:mod_rewrite - exclude urls但不起作用!

我有下一个:

RewriteCond %{REQUEST_URI} !^/user/registrationpassword(/.*)?$ [NC]
RewriteRule ^.*$ https://qat.example.com/ [R,NC,NE,L]

重定向所有 网址。 我需要从重定向中排除/user/registrationpassword/

重定向示例:

www.example.com                                   -> qat.example.com
www.example.com/foo                               -> qat.example.com
www.example.com/test/foo                          -> qat.example.com
www.example.com/user                              -> qat.example.com
www.example.com/user/registrationpasswordX        -> qat.example.com
www.example.com/user/registrationpasswordX/foo    -> qat.example.com
www.example.com/user/registrationpassword         -> qat.example.com/user/registrationpassword
www.example.com/user/registrationpassword/foo     -> qat.example.com/user/registrationpassword/foo
www.example.com/user/registrationpassword/foo/bar -> qat.example.com/user/registrationpassword/foo/bar

Apache版本:

$ httpd -version
Server version: Apache/2.4.16 (Amazon)
Server built:   Aug 13 2015 23:52:1

1 个答案:

答案 0 :(得分:0)

尝试使用\b字符:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/user/registrationpassword\b [NC]
RewriteRule ^.*$ https://qat.example.com/ [R,L]

PS:您当然不需要NC,NE个标记,因为没有任何内容可以转义,或者比较大小写。

您也可以尝试以下方法:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(?!user/registrationpassword\b).*$ https://qat.example.com/ [R,L]

完成后,需要添加/user/registrationpassword的重定向:

RewriteRule ^.*$ https://qat.example.com/$0 [R,L,NE,QSA]