Something like this?
And see also this for some additional info.
The basic info is these rules
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)/ $1?%1 [L]
will rewrite the following
/mypage/param1/val1/param2/val2/param3/val3/... --->
/mypage?param1=val1¶m2=val2¶m3=val3&...
It takes the first parameter and calls that page. The rest of the parameters are turned into the query string.
EDIT with more about how the code works.
The first line (and the third) merely capture everything after the "?" so that it can be added back to the end of the rewritten URL. The same thing
can be accomplished with the [QSA] flag, but I prefer to do it
explicitly so it's visible.
The string is saved in the %1 variable.
The second line uses a regular expression to split the URL into three parts.
$2 and $3 are the last two parts of the path.
$1 is everything before that.
So this:
/mypage/param1/val1/param2/val2/param3/val3/param4/val4/param5/val5/
Becomes this:
$1 $2 $3
/mypage/param1/val1/param2/val2/param3/val3/param4/val4/ param5 val5
and is rewritten as this (spaces added for readability):
/mypage/param1/val1/param2/val2/param3/val3/param4/val4/ ? param5 = val5
If your URL uses separators other than slashes, you can modify the regular
expression accordingly.
The [L] ("last") flag prevents the rest of the rules from running.
HOWEVER -- and this is the key -- mod_rewrite calls the webserver with the new
URL and the new URL is sent through mod_rewrite again! This is sometimes called mod_rewrite recursion and is exactly what you want for this to work.
The next time through, the next two parts are stripped off the end and added
to the query string.
This:
$1 $2 $3 %1
/mypage/param1/val1/param2/val2/param3/val3 param4 val4 ? param5=val5
Becomes this:
/mypage/param1/val1/param2/val2/param3/val3 ? param4 = val4 & param5=val5
This keeps on happening until all the parts of the URL are converted into
parts of the query string. When that happens, the second line doesn't match
but the fourth line does. That is what gives you your final URL to call the
program you want. The links above show answers to slightly different variations
that allow for different rules for that.
Also note that there are limits to the number of times mod_rewrite
can recurse. See the settings for
MaxRedirects
in .htaccess and
LimitInternalRecursion
in your apache conf file.