regex - 排除 URL 的重定向匹配不起作用

标签 regex apache .htaccess redirect mod-alias

我使用 RedirectMatch 规则,该规则应排除以下两个 URL:

  • citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen
  • citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen

我将此规则与正则表达式一起使用,但收到 500 内部服务器错误:

RedirectMatch 301 /citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$ /citycards/citycards-locations/muenchen/$1

有什么想法为什么它不起作用吗?

最佳答案

您当前的规则是:(分为多行以便更好地显示/理解):

RedirectMatch
301
/citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$
/citycards/citycards-locations/muenchen/$1

基本上,您的正则表达式表示:

  • 匹配/citycards/citycards-locations/muenchen/
  • 后面没有跟随以下内容

    1. citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen
    2. citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen(18‌​-munchen 之后有一个空格>)
  • 匹配所有内容,直到 URI 结尾

并将匹配的 URI 重定向到:/citycards/citycards-locations/muenchen/$1,这与匹配的 URL 基本相同。

我看到 2 个问题。

  1. 如果否定前瞻中的空格不被视为模式的一部分,则实际上是将 4 个参数传递给 RedirectMatch 指令,从而导致状态 500 错误
  2. 如果模式解析正确,则存在无限重定向循环。

关于regex - 排除 URL 的重定向匹配不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33867891/

相关文章:

ruby - 在扫描或正则表达式模式中的模式中提取 Ruby 字符串

python - django 1.6.5 + python3 wsgi 问题

apache - 如何将 .htaccess 重定向到两个 MVC 树

regex - .htaccess mod_rewrite 超薄路由

php - 干净的 URL 用户名重定向

c# - 正则表达式性能缓慢

ruby - 如何创建一个与 Ruby 中除某些字符串之外的模式匹配的正则表达式?

python - 制作 Markdown 解析器时的正则表达式问题

linux - 尝试在 CentOS 7 上的 Apache 上安装 GitLab

node.js - 使用 htaccess 重定向到 nodejs 应用程序