我需要在 urlrewrite 规则中将一些数字放入生成的 url 中,因此来自 url like “/test/723/”我会得到“/test/700-series/723/” 显然这
RewriteRule ^/test/([1-9])([0-9]{2})/$ /test/$100-series/$1$2/
不起作用,因为无法正确解释 $100。有什么方法可以将 $1 与其后面的任何数字分开吗?
最佳答案
你很接近,只需删除前导斜线:
RewriteRule ^test/([1-9])([0-9]{2})/?$ /test/$100-series/$1$2/ [L]
.htaccess
是每个目录指令,Apache 从RewriteRule
URI 模式中去除当前目录路径(因此前导斜杠)。
根据 official Apache doc它明确指出:
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 个零。
关于regex - urlrewrite 中 $1 之后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829112/