I’m trying to use mod_rewrite to handle an unknown number of variables.
An example URL would be:
example.com/var1-var2-var3/title
I have this so far:
RewriteRule ^([^/.]+)-([^/]*)(.*)$ $3?version[]=$1&version[]=$2 [QSA,N]
RewriteRule ^([^/.]+)/?$ ?title=$1 [QSA,N]
RewriteRule ^/?$ /index.php [QSA,L]
This returns:
Array ( [title] => title [version] => Array ( [0] => var1-var2 [1] => var3 ) )
I need it to return: Array ( [title] => title [version] => Array ( [0] => var1 [1] => var2 [2] => var3 ) )
I don’t think you can capture an arbitrary number of arguments in a single RewriteRule regex. Wouldn’t it be easier to redirect to
index.php?version=var-var2-var3then in PHP do anexplode()on$_POST['version']?EDIT: You can do it for a bounded number of vars (9 is the maximum captured subgroups I believe) and then remove redundant empty entries. Messier then the simple explode() alternative in my opinion, but here you go:
You can also do a complicated loop by moving one var to the new format on each run of the rewriting engine and go on running it until you run out of vars, but I think that’s more than a URL rewriting engine should be responsible for.
EDIT 2: Ok, here’s the loop I mentioned:
It transform a sample URL as follows: