Apache mod_rewrite:替换查询字符串参数值
我需要使用apachemod_rewrite替换QueryString的值。许多值必须替换为一个新值({oldValueA1,oldValueA2}=>newValueA;{oldValueB1,oldValueB2}=>newValueB)。该参数在查询字符串中显示可变时间。参数值的顺序是随机的。如果重定向查询字符串包含一个值两次,对我来说无关紧要。 请求uri是可变的,但在重定向期间不应更改 这里有一些例子。第一行包含原始url,第二行包含新urlApache mod_rewrite:替换查询字符串参数值,apache,url,mod-rewrite,redirect,query-string,Apache,Url,Mod Rewrite,Redirect,Query String,我需要使用apachemod_rewrite替换QueryString的值。许多值必须替换为一个新值({oldValueA1,oldValueA2}=>newValueA;{oldValueB1,oldValueB2}=>newValueB)。该参数在查询字符串中显示可变时间。参数值的顺序是随机的。如果重定向查询字符串包含一个值两次,对我来说无关紧要。 请求uri是可变的,但在重定向期间不应更改 这里有一些例子。第一行包含原始url,第二行包含新url localhost/mag/cat/?my
localhost/mag/cat/?myParameter=oldValueA1&myParameter=oldValueB1
localhost/mag/cat/?myParameter=newValueA&myParameter=newValueB
localhost/mag/?myParameter=oldValueA2
localhost/mag/?myParameter=newValueA
localhost/dig/cat/?myParameter=oldValueB1&myParameter=oldValueA2
localhost/dig/cat/?myParameter=newValueB&myParameter=newValueA
localhost/dig/?myParameter=oldValueB2&oldValueB3
localhost/dig/?myParameter=newValueB&newValueB
我尝试了几种有条件的重写规则,但我找不到解决这个问题的方法。有人有主意吗
Thx!:) 我找到了解决问题的办法。也许它也会帮助其他人。这是:
RewriteCond %{QUERY_STRING} ^(.*[&?]|)myParameter=(oldValueA1|oldValueA2)([&?].*|)$
RewriteRule ^(.*)$ $1?%1myParameter=newValueA [R=301,NC,L]
RewriteCond %{QUERY_STRING} ^(.*[&?]|)myParameter=(oldValueB1|oldValueB2)([&?].*|)$
RewriteRule ^(.*)$ $1?%1myParameter=newValueB [R=301,NC,L]
不相关:当您有多个参数时,它们应该用
和分隔,而不是?
重写查询字符串参数非常困难,请查看此答案以了解详细信息。正如您所看到的,您将遇到很多问题(比如使用尚未编码的字符),也许您可以在应用程序代码中尝试它,而不是apache/mod_rewriteThx,您对这个问题的看法如何?及&。复制粘贴错误。我将更改它。如果您通过localhost/mag/cat/?myParameter=newValueA&myParameter=newValueB,则只有最后一个,即myParameter=newValueB传递给您的CGI脚本。@jacouh我在下面的回答中用解决方案测试了它,两个值都通过了。它很好用。