Search code examples
apache.htaccesshttp-redirectmod-rewritejoomla

htaccess Redirect for duplicate parameter in the URL


There are certain unwanted URLs which are having duplicate parameters. The value for parameters is 1 and 0 (both examples given below). I am trying to 301 redirect them to main URLs i.e. get rid of the parameter part.

Example 1:

www.xyz.com/2012/04/bharati-4g-tariff-and-rate-bharati-4g.html?m=0?m=1

redirect to

www.xyz.com/2012/04/bharati-4g-tariff-and-rate-bharati-4g.html

Example 2:

www.xyz.com/2012/04/bharati-4g-tariff-and-rate-bharati-4g.html?m=1?m=1

redirect to

www.xyz.com/2012/04/bharati-4g-tariff-and-rate-bharati-4g.html

One short solution but not satisfactory: I had the solution where I could show 404 not found error for these URLs but later this started giving me error in some admin pages in my Joomla site. The solution I followed is:

RewriteCond %{QUERY_STRING} m [NC]
RewriteRule ^(.*)/?$ /$1? [R=404,L]

Please suggest me solution which will give 301 redirect to base urls or will give https 404 not found header code.

The solution shall affect only URLs which have duplicate/double URLs like shown in example above but not other types.


Solution

  • You can use this redirect rule as very first rule in your .htaccess to remove unwanted query string:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^m=[^&]* [NC]
    RewriteRule ^ %{REQUEST_URI}? [R=302,L]