So I’m working on rewrite rules presently, and I’m essentially wanting to do the following:
\/([\w+-]+)\/([\w+-]+)\/([\w+-]+)\/([\w+-]+)\/([\w+-]+)...
As you can see, I’m wanting to match the same pattern over and over again, which I could just write out however many times will suit my purposes at present, but that’s not very elegant. Since it’s the same pattern for each capture, it would be nice if this could be written in an arbitrary manner, something like the following:
(?:\/([\w+-]+))+
Then ideally, I could assign the rewrite as follows:
$1.php?one=$2&two=$3...
Anyone know if this is possible? May not be, but never hurts to ask.
This idea is called repeated capture group, and it’s not possible. Imagine it like this: the captured characters of
([\w+-]+)are put in a buffer, and the second match simply overwrites the first.A practical solution would be to just capture the whole thing, and handle it in php (or any other serverside script):
then in PHP:
Alternatively… well, this is grossly impractical, but you could do it with mod_rewrite by using recursion: every iteration would recognize the last segment, pass it to the query_string etc. It wouldn’t work in production (as Apache sanely has the recursion limit set to something low), but it’s a nice reminder that mod_rewrite is Turing complete.