ahnbizcad ahnbizcad - 3 months ago 9
Apache Configuration Question

Pass all other query params in apache htaccess rewrite / redirect?

I want to match certain query params, and redirect to some new url but not lose all other query params (e.g. utm, etc)

For instance if the request is

home.com/request?a=1&utm=stuff


I want to redirect to

home/com/target?utm=stuff


not

home/com/target?&utm=stuff


I know $1 $2 etc refer to groups of the rewrite rule,
and %1 %2 refer to groups of the query string.

The following

RewriteCond %{REQUEST_URI} ^request$
Rewritecond %{QUERY_STRING} ^(.)a=1(.*)$
RewriteRule .* /?%1%2 [L,R=302]


subtracts the
a=1
, but does not handle the & smartly.

Answer

You can use this redirect rule:

RewriteCond %{QUERY_STRING} ^(.*&)?a=1(?:&(.*))?$ [NC]
RewriteRule ^request/?$ /target?%1%2 [L,NE,R=302]