Search code examples
apache.htaccessmod-rewriteparameters

redirect with multiple parameter query strings htaccess


I want to redirect an url with multiple parameters and rename the parameters keys:

From

 www.site.it/index.php?par1=1&lang=en_US

to

 www.site.com/index.php?parameter1=1&languages=en_US

I tried with this:

RewriteCond %{QUERY_STRING} par1=([0-9]+)
RewriteCond %{QUERY_STRING} lang=en_US
RewriteRule ^index.php site.com/index.php?parameter1=$1&lang=en_US [R=301,L]

But it failed.


Solution

  • You can use:

    RewriteCond %{QUERY_STRING} par1=(\d+)&lang=([^&]+)
    RewriteRule ^index\.php http://www.example.com/index.php?parameter=%1&languages=%2 [R,L]
    

    %1 and %2 are references to the capture groups in the query string regex