Reputation: 7693
I'm doing this in .htaccess:
RewriteRule ^([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2&u3=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)[/]?$ /index.php?u1=$1&u2=$2&u3=$3&u4=$4 [L,QSA]
Is there any way to make this automatically from u1 to u(infinite), automatically, based on the length of the url, without having to define every case?
Upvotes: 0
Views: 79
Reputation: 13169
No, neither Apache nor regex offer programmatic processing of an unspecified number of arguments. But PHP is designed for such things, so you're best off simply using one rule:
RewriteRule ^([A-Za-z0-9/-]+)$ /index.php?path=$1
Then have your PHP script break the path
variable apart by calling the explode function on the forward-slash character. And you'll get an array containing each piece of the full path.
This way your PHP script can handle an unlimited number of path elements, and Apache won't need to wear itself out trying to make sense of infinite regex patterns.
Upvotes: 2