If you just have 3 path segments your current code does not seem that bad. Your current rules are easy to understand, and I don't think the duplicated conditions !-f
and !-d
will have a huge performance impact.
You can rewrite your current rules to a sequence like this. First you rewrite the url val1/val2/val3/val4/val5
to index.php/val1/val2/val3/val4/val5
. Then we have a bunch of similar rules that all take the first path segment and turn it into a parameter. Once no more path segments remain the rest of the rules are ignored.
#Only on requests with at least 1 character (e.g. not to http://localhost)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php%{REQUEST_URI} [QSA]
#Since we don't have the L flag, this will do param1 - paramx in one go
RewriteRule ^index\.php/([^/]+)(/.*)?$ index.php$2?param1=$1 [QSA]
RewriteRule ^index\.php/([^/]+)(/.*)?$ index.php$2?param2=$1 [QSA]
RewriteRule ^index\.php/([^/]+)(/.*)?$ index.php$2?param3=$1 [QSA]
RewriteRule ^index\.php/([^/]+)(/.*)?$ index.php$2?param4=$1 [QSA]
RewriteRule ^index\.php/([^/]+)(/.*)?$ index.php$2?param5=$1 [QSA]
Before using this, you should test both the performance of these rules and your current rules to see which one performs better. Please note that this approach yields strange results if more than, in this case, 5 path segments are in the url, as the 6-th until nth path segment will overwrite param1 - param5. You could 'fix' this by adding another rule RewriteRule ^index\.php index.php [END]
on recent versions of Apache.