mod_rewrite to change query string parameter name

纵然是瞬间 提交于 2019-12-25 06:36:49

问题


I need help writing a mod rewrite rule to change the name of a query string parameter. I want to change the name, not the value.

old name partner new name a_aid

so a link like this

http://domain.com/?partner=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo

will become

http://domain.com/?a_aid=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo

I found this article but the accepted answer generated errors for the OP: mod_rewrite - old parameter name to new name

also this article, but the solution was to use PHP. which will not work in my case: APACHE mod_rewrite change variable name in query string

I can't use PHP because some affiliate tracking code creates a cookie from the query string--and expects the a_aid. So I'm trying to convert partner into a_aid for it


回答1:


OK think I hacked it together on my own. Please post an answer if you think its brittle or could be done better and I'll accept yours instead

RewriteCond %{QUERY_STRING} ^(.*)partner(.*)$
RewriteRule ^(.*)$ $1?%1a_aid%2 [R=301,L]


来源:https://stackoverflow.com/questions/15005384/mod-rewrite-to-change-query-string-parameter-name

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