Well this is sort of an experiment, it will work as you have said but I believe there may be a better way to do this:
RewriteCond %{REQUEST_FILENAME} !/(index\.php|_modules|css|files|ico|img|js)/
RewriteRule ^(([^/]*)/?)(([^/]*)/?)(([^/]*)/?)(([^/]*)/?)$ /index.php?p1=$2&p2=$4&p3=$6&p4=$8 [L]
Basically I am adding an extra group per entry so you can safely exclude a non existent group.
So the first element is required and is $2
, the second element becomes $4
, the third element becomes $6
and the last element becomes $8
.
(([^/]*)/?)
this basically means that anything not a /
that ends or not with /
for the outer group and inner group anything not a /
.
Simplified the rule a bit more:
RewriteCond %{REQUEST_FILENAME} !/(index\.php|_modules|css|files|ico|img|js)/
RewriteRule ^([^/]*|)/?([^/]*|)/?([^/]*|)/?([^/]*|) /index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]