【发布时间】:2013-11-17 18:14:45
【问题描述】:
我需要使用 Apache mod_rewrite 替换 QueryString 的值。许多值必须被一个新值替换({oldValueA1, oldValueA2} => newValueA; {oldValueB1, oldValueB2} => newValueB)。该参数在 query_string 中出现的次数不定。参数值的顺序是随机的。如果重定向的 query_string 包含两次值,对我来说没关系。 request_uri 是可变的,但在重定向期间不应更改。
这里有一些例子。第一行包含原始网址,第二行包含新网址。
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
我尝试了几个带有某些条件的 RewriteRules,但我找不到这个问题的解决方案。有人有想法吗?
谢谢! :)
【问题讨论】:
-
不相关:当您有多个参数时,它们应该用
&分隔,而不是? -
重写查询字符串参数真的很难,详情请查看此答案stackoverflow.com/questions/7703552/rewriting-dynamic-urls/…。如您所见,您将遇到很多问题(例如使用尚未编码的字符),也许您可以在应用程序代码中尝试它而不是 apache/mod_rewrite
-
谢谢,您对 ?和 &。复制粘贴错误。我会改的。
-
如果您传递 localhost/mag/cat/?myParameter=newValueA&myParameter=newValueB,则只有最后一个,即 myParameter=newValueB 被传递给您的 CGI 脚本。
-
@jacouh 我在下面的答案中使用解决方案对其进行了测试,并且两个值都通过了。它工作正常。
标签: apache url mod-rewrite redirect query-string