Question

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

Was it helpful?

OTHER TIPS

If @superspace's latest answer doesn't work, what I would suggest is routing all links that are not to direct files/directories and route them to an index page. Then setup a routing class which takes in the page name and does manual matching, so you can have your named capture regex array and list the templates or pages you want to feed.

If you have to go this way, let me know and I can offer some code from my classes.

No backreferences it seems, after looking into the mod_rewrite source.

I'd recommend using the RewriteMap option anyway instead of a long list of RewriteRules, as it will be much faster than iterating through a lengthy list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top