views:

24

answers:

3

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

+1  A: 

THis might be useful:

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

superspace
It's certainly a way of achieving the desired result, but as I stated in the question I have an inline solution already in place using the numerical references. My question is pointed directly to the use of PCRE's named backferences inline, can they be used, and if so, why doesn't my RewriteRule work.
flungabunga
I think named rewrites are not supported. There are only four methods under "RewriteCond Directive": 1. RewriteRule backreferences 2. RewriteCond backreferences 3. RewriteMap expansions (my suggestion) 4. Server-Variables. Only two of it is non-numeric. And of the non-numeric methods it does not do what you are trying to do.
superspace
Yeah I read the same in the doco and sounds fair as the backference N is of the set (1<=N<=9), no mention of named backreference. (it's the lack of mention that irks me). Raised.
flungabunga
Yay got my first up-vote! Thanks flungabunga
superspace
Confusion derived from documentation stating that mod_rewrite uses PCRE. Solution derived from documentation as it states that the backreference label is N where (1<=N<=9). So it would be safe to assume that named backreferences are _not_ supported in inline.
flungabunga
A: 

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.

meder
Have used such classes in the past in various frameworks. Especially handy for when mod_rewrite isn't available, or your using a webserver which is managed and the support team are slooooooow. But yeah, I have a simple inline solution in place, but 'twould be awesome to know if named backreferences are available and if so how.
flungabunga
A: 

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.

Sasha