1

All,

I've come across an interesting little quirk in one of my RewriteRules, which I wanted to resolve by the use of named back references. However from what I can see, this is not possible in Apache's mod_rewrite.

I have two incoming urls, each containing a key variable, which need to be rewritten to the same underlying framework action.

Incoming urls:

/users/list/page-2
/users/list/2

Desired rewrite endpoint

/?module=users&action=list&pagenum=2

I would have liked to do something like this

RewriteRule ^/(?P<module>([\w]+))/(?P<action>([\w]+))/(page-)?(?P<pagenum>([\d]+))$ /?module=${module}&action=${action}&pagenum=${pagenum} [L,QSA]

However Apache just doesn't want to play like that at all, and gives me null values in the places of the named backreferences. To get me round the problem I've used numerical references to the captured groups ($1, $2, $4)(but I'm almost halfway to the N=9 apache limit). So this isn't a show stopper for me.

I would just like to know, if named backreferences are available in Apache's mod_rewrite, and if they are, why does my RewriteRule's pattern not match?

Thanks, Ian

4

3 回答 3

1

这可能有用:

https://httpd.apache.org/docs/trunk/rewrite/rewritemap.html

于 2010-10-27T00:32:42.677 回答
0

在查看 mod_rewrite 源代码后,似乎没有反向引用。

无论如何,我建议使用 RewriteMap 选项而不是一长串 RewriteRules,因为它比遍历一个冗长的列表要快得多。

于 2010-10-27T05:29:47.720 回答
0

如果@superspace 的最新答案不起作用,我建议将所有不指向文件/目录的链接路由到索引页面。然后设置一个路由类,它接受页面名称并进行手动匹配,这样您就可以拥有命名的捕获正则表达式数组并列出您想要提供的模板或页面。

如果您必须这样做,请告诉我,我可以提供一些课程中的代码。

于 2010-10-27T00:41:10.637 回答