【问题标题】:Apache mod_rewrite mapping path to parametersApache mod_rewrite 映射路径到参数
【发布时间】:2020-11-25 20:33:40
【问题描述】:

我正在从 IIS 迁移到 Apache(在 Windows 上)并努力适应重写规则(在 IIS 中使用 Helicon ISAPI_Rewrite 3)。

该规则将看起来像目录结构路径的内容映射回一组查询字符串参数。路径中可以有任意数量的参数。

例如

/basket/param1/value1/param2/value2/param3/value3 ...等等...

变成……

/basket?param1=value1&param2=value2&param3=value3 ...等等...

ISAPI_Rewrite 中的规则:

#  This rule simply reverts parameters that appear as folders back to standard parameters
#  e.g. /search-results/search-value/red/results/10 becomes /search-results?search-value=red&results=10

RewriteRule ^/(.*?)/([^/]*)/([^/]*)(/.+)? /$1$4?$2=$3  [NC,LP,QSA]

我首先发现 Apache 没有 'LP' 标志,因此将其换成 N=10 作为循环测试...

RewriteRule ^(.*?)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N=10,QSA]

但是,Apache 错误日志显示一遍又一遍地添加相同的参数,直到达到 N 标志上的循环数,以 HTTP 500 错误结束。

有什么想法我哪里出错了吗?!?

【问题讨论】:

    标签: regex apache mod-rewrite url-rewriting isapi-rewrite


    【解决方案1】:

    在做了很多挠头并使用我的 Google Foo 之后,我在另一个 Stack Overflow 评论中找到了我所有问题的解决方案......

    https://stackoverflow.com/a/5520004/14054970

    基本上...

    显然 mod_rewrite 重新附加存在问题 某些情况下的后修复部分 https://issues.apache.org/bugzilla/show_bug.cgi?id=38642

    问题:

    如果 .htaccess 文件中的多个 RewriteRules 匹配,则不需要 PATH_INFO 的副本可能会累积在 URI 的末尾。

    如果您使用的是 Apache 2.2.12 或更高版本,则可以使用 DPI 标志 防止这种http://httpd.apache.org/docs/2.2/rewrite/flags.html

    我使用的是 Apache 2.4,所以我的重写规则现在如下所示(为了安全起见,我将在所有规则中添加 DPI 标志)...

    RewriteRule ^(.*?)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3  [NC,N=1000,QSA,DPI]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多