regex - urlrewrite 中 $1 之后的数字

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

我需要在 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/

相关文章:

java - 从字符串数组中删除正则表达式

python - python中的正则表达式-不间断地查找一组数字

apache - 如果出现 404,则 Apache 不提供 CORS header

regex - 使用新 url 结构从一个域 301 重定向到新域的正确方法?

.htaccess - Htaccess 用于重定向不存在的子页面

regex - 使用 sed 捕获的组变量作为 bash 命令的输入

c# - 正则表达式 -> 只有字母并以点结尾

php - 无法写入新创建的文件

php - Laravel 环境困惑

php - 在此服务器上找不到请求的 URL/登录名