Reference capture groups of multiple RewriteCond in RewriteRule

You could try constructing the target URL inside the rewrite conditions:

RewriteCond ##%{QUERY_STRING}      (.*)##(|.*&)param1=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param2=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param3=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param4=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param5=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param6=([^&]+)

RewriteRule ^foo$ /bar%1/%3? [L,R]

When I try to request:

/foo?param1=a&param2=b&param6=3&param3=4&param5=5&param4=6

I get redirected to:

/bar/a/b/4/6/5/3

Adding any additional required query string parameters won’t make it look any more messy than it already is.

Leave a Comment