【问题标题】:Combine RedirectMatch and RewriteRule结合 RedirectMatch 和 RewriteRule
【发布时间】:2015-07-07 05:13:31
【问题描述】:

我想将重定向与重写结合起来。 重写应该匹配完整路径,但重定向应该只匹配子路径。

网址:http://localhost/mypath/google

预期:重定向到http://www.google.com/

实际:重写为http://stackoverflow.com

RewriteEngine On
RedirectMatch ^/mypath/google/(.*) http://www.google.com/
RewriteRule   ^/mypath/(.*)$  http://stackoverflow.com [L,P]

显然 RewriteRule 总是匹配的。是否可以更改规则,以便首先检查 RedirectMatch 规则?

【问题讨论】:

  • 正则表达式和目标 URL 周围的引号是什么意思...?
  • 感谢您的提示。但报价没有任何区别。不过,为了简单起见,我只是编辑了问题。
  • 不确定这两个指令是否真的按照写入的顺序进行评估(毕竟它们是不同模块的一部分)——我建议你只使用 RewriteRules 来做这两件事。跨度>
  • 谢谢!这就是解决方案:RewriteRule .... [R]
  • 好的,那我补充一下。

标签: apache mod-rewrite


【解决方案1】:

在处理请求期间,Apache 会以特定顺序调用其不同的模块——因此您不能假设您以特定顺序在配置文件中编写的所有指令也将按该顺序“执行”。

由于RedirectMatchRewriteRule 来自不同的模块,我想这里就是这种情况——首先处理您的RewriteRule,然后重写以/mypath/ 开头的所有内容,因此您的RedirectMatch 没有有机会再跳上那辆火车……

所以最简单的解决方案是使用 RewriteRules 处理 both 重定向,首先处理更具体的路径,然后使用您已有的第二条规则捕获其余路径。

RewriteEngine On
RewriteRule ^/mypath/google/(.*) http://www.google.com/ [R]
RewriteRule ^/mypath/(.*)$  http://stackoverflow.com [L,P]

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多