【问题标题】:.htaccess [OR] condition not met causes unexpected RewriteRule.htaccess [OR] 条件未满足导致意外的 RewriteRule
【发布时间】:2019-07-13 08:10:38
【问题描述】:

我们有一个非常大的 .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 文件语法看起来不错(在两个不同的语法检查工具中检查)。

怎么会这样?

感谢您的帮助。

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite apache2


    【解决方案1】:

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

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

    总结如下: https://serverfault.com/a/214521/59126

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多