【问题标题】:Percentage sign inside .htaccess mod_rewrite.htaccess mod_rewrite 内的百分比符号
【发布时间】:2013-10-14 04:34:45
【问题描述】:

当我在 mod_rewrite 正则表达式中包含百分号 (%) 时,我的服务器不断抛出 404 错误。我到处搜索都无济于事。我正在运行 apache 2.4.4 并尝试了如下所示的 B 标志。

RewriteRule ^pages/([a-zA-Z]+)/([a-zA-Z+%'"]+)$ pages/$1.php?search=$2 [B]

因此,当我访问 pages/animals/cats%20and%20dogs 时,它会失败。但是当我将 ([a-zA-Z+%'"]+) 更改为 (.*) 时,它就像一个魅力

正则表达式级别 = 蹒跚学步,非常感谢任何帮助。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite escaping


    【解决方案1】:

    %20 在 mod_rewrite 中使用 \s(空格)匹配。因此,将您的规则更改为:

    RewriteRule ^pages/([a-zA-Z]+)/([a-zA-Z+\W'"]+)$ pages/$1.php?search=$2 [B,L]
    

    【讨论】:

    • 当 %20 在 url 中时,用 "\s" 替换 "%" 仍然失败。我还需要 %2C 和 %40 (, @) 的百分号
    • 您需要了解 %2C%40 字符等是在 URL 到达 Apache 之前进行编码的。请使用 \W 查看编辑后的答案以匹配所有这些案例
    • 啊,我明白了!非常感谢,除了(%2F, %5C) (/, \),所有特殊字符现在都可以使用。
    • 哦,好的,我会在一小时后回到我的电脑前进行调查。
    • 全部修复。我不得不将AllowEncodedSlashes On 附加到我的httpd.conf 文件中。现在所有编码的字符都很好地播放。谢谢阿努巴瓦。
    猜你喜欢
    • 2013-12-15
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多