.htaccess [OR]条件不满足会导致意外的RewriteRule

时间:2019-02-19 18:29:01

标签: apache .htaccess redirect mod-rewrite apache2

我们有一个很大的.htaccess文件,其中我们根据不同的域RewriteCond创建RewriteRules。我们使用[OR]标志来确保同时满足“ www”和域条件,然后重定向到特定页面。

示例:

# DOMAIN 1
RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
RewriteRule ^en/contact$ https://domain1.com/en/contact.html [L,R=301]
RewriteRule ^fr/nouvelles$ https://domain1.com/fr/articles.html [L,R=301]
RewriteRule ^en/career$ https://domain1.com/en/career.html [L,R=301]
RewriteRule ^fr/contact$ https://domain1.com/fr/contact.html [L,R=301]

# DOMAIN 2
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^fr/maison-payante$ https://domain2.com/fr/articles.html [L,R=301]
RewriteRule ^en/cash-damming$ https://domain2.com/en/articles.html [L,R=301]
RewriteRule ^fr/nos-proprietes$ https://domain2.com/fr/nos-proprietes.html [L,R=301]
RewriteRule ^en/40km-program$ https://domain2.com/en/articles.html [L,R=301]
RewriteRule ^fr/mes-proprietes$ https://domain2.com/fr/nos-proprietes.html [L,R=301]
RewriteRule ^en/our-properties$ https://domain2.com/en/our-properties.html [L,R=301]

问题

访问http://domain1.com/en/40km-program将重定向到https://domain2.com/en/articles.html

即使不满足RewriteCond,也会应用以下规则:

RewriteRule ^en/40km-program$ https://domain2.com/en/articles.html [L,R=301]

.htaccess文件的语法似乎不错(已在两个不同的语法检查器工具中进行了检查)。

这怎么可能发生?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

似乎不能有多个RewriteCond不能被多个RewriteRule跟随:

RewriteCond指令定义规则条件。一个或多个RewriteCond可以位于RewriteRule指令之前。然后,仅当URI的当前状态都与它的模式匹配并且满足这些条件时,才使用以下规则。 https://httpd.apache.org/docs/2.2/en/mod/mod_rewrite.html

这就是全部内容: https://serverfault.com/a/214521/59126