【问题标题】:number after $1 in urlrewriteurlrewrite 中 $1 后的数字
【发布时间】:2014-01-16 17:21:19
【问题描述】:

我需要在 urlrewrite 规则中将一些数字放入结果 url,所以从 url 像 “/test/723/”我会得到“/test/700-series/723/” 很明显这个

RewriteRule ^/test/([1-9])([0-9]{2})/$  /test/$100-series/$1$2/ 

不起作用,因为无法正确解释 $100。有什么方法可以将 $1 与其后面的任何数字分开吗?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    你已经很接近了,只需删除前导斜杠:

    RewriteRule ^test/([1-9])([0-9]{2})/?$  /test/$100-series/$1$2/ [L]
    
    • .htaccess 是每个目录指令,Apache 从 RewriteRule URI 模式中删除当前目录路径(因此是前导斜杠)。

    根据official Apache doc,它明确指出:

    RewriteRule 反向引用:这些是 $N (0

    Hence $100 is always interpreted as $1 and 2 literal zeroes.

    【讨论】:

    • 我可以知道为什么在这里投反对票。 (我在发布答案之前已经测试过了)
    • 问题是如何让 Rewrite_Mod 只计算 1 美元,而不是 100 美元。
    • 如果您测试我发布的答案,您会注意到它正在根据 OP 的需要将 /test/723/ 重写为 /test/700-series/723/
    • 如果您愿意阅读文档:httpd.apache.org/docs/current/mod/mod_rewrite.html 它明确指出 RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9). $1 to $9 provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions. $0 provides access to the whole string matched by that pattern. 因此,$100 始终被解释为 $1 和 2 个文字零。
    • 您是对的,请在您的回答中引用文档以解决问题。 (你必须编辑你的答案,这样我才能撤消我的反对意见)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    相关资源
    最近更新 更多