重写条件只运行第一个条件

时间:2014-11-14 15:21:57

标签: apache .htaccess mod-rewrite url-rewriting url-redirection

staging.domain.com应该转到domain.com/blog(最后一条规则),但它会转到domain.com staging.domain.com/category/food应该去domain.com/blog/category/food

RewriteEngine On

RewriteCond %{HTTP_HOST} ^staging.domain.com/category [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^staging.domain.com/work/ [NC]
RewriteRule ^(.*)$ http://domain.com/#work [R=301,NC]


RewriteCond %{HTTP_HOST} ^staging.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/blog [R=301,NC]

如何制定多条规则,或者我将它们链接错误

1 个答案:

答案 0 :(得分:1)

变量%{HTTP_HOST}仅包含HTTP request header。 URL部分不可用于此变量。因此,必须按如下方式更改规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^staging.domain.com$ [NC]
RewriteRule ^((?!work/?).*)$ http://domain.com/blog/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^staging.domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/work/? [NC]
RewriteRule .* http://domain.com/#work [R=301,NC]