301 redirect old parameter names to new parameter

2019-08-20 19:04发布

问题:

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.

回答1:

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]


回答2:

Try :

RewriteEngine on

RewriteCond %{THE_REQUEST} \?colornew=([^&]+)&productname=([^&\s]+)
RewriteRule ^ %{REQUEST_URI}?color=%1&product=%2 [QSA,NC,NE,L,R=301]