Search code examples
apache.htaccessmod-rewritehttp-redirect

301 redirect old parameter names to new parameter names by htaccess


I just changed two parameter names and wanna redirect old names to changed name ones with any values anywhere in URL. e.g:

product.php?colornew=anyvalue&productname=anyvalue

301 redirect to:

product.php?color=anyvalue&product=anyvalue

Please note that this is just an example and as I said these two parameter can be anywhere in URL with any value.


Solution

  • You can use this code to rename your query parameters in any URL:

    RewriteEngine On
    
    # rename query parameter colornew=>color
    RewriteCond %{QUERY_STRING} ^(.*&)?colornew=([^&]*)(&.*)?$ [NC]
    RewriteRule ^ %{REQUEST_URI}?%1color=%2%3 [NC]
    
    # rename query parameter productname=>product
    RewriteCond %{QUERY_STRING} ^(.*&)?productname=([^&]*)(&.*)?$ [NC]
    RewriteRule ^ %{REQUEST_URI}?%1product=%2%3 [NC,NE,L,R=302]