301 redirect old parameter names to new parameter names by htaccess

橙三吉。 提交于 2019-12-12 04:18:58

问题


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]


来源:https://stackoverflow.com/questions/34548049/301-redirect-old-parameter-names-to-new-parameter-names-by-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!