0
背景如何用RewriteCond和RewriteRule删除部分查询字符串?
我想在URL中替换几个(3)查询字符串参数。我们正在转向一个新的搜索引擎,并且还有很多我们想要继续工作的链接。例子:
http://example.com/search?q=s&restrictBy[foo]=fltr1&restrictBy[baz]=fltr2 ->
http://example.com/search?q=s&newfoo=fltr1&bazinga=fltr2
http://example.com/search?q=s&restrictBy[bar]=fltr3 ->
http://example.com/search?q=s&barista=fltr3
http://example.com/search?q=s&restrictBy[bar]=fltr1&restrictBy[baz]=fltr2&restrictBy[foo]=fltr3 ->
http://example.com/search?q=s&barista=fltr1&bazinga=fltr2&newfoo=fltr3
问题
的第一个参数重写规则does not look at the query string,所以在保持URL的其余部分完好的,我不能没有一个参数。此外,事实上可能存在NONE,SOME或ALL的所有参数,并且IN ANY ORDER正在将我投入循环。
# Does not work :/
RewriteRule (.*)restrictBy\[foo\]=(.*) $1newfoo=$2
# Kinda works, but loses rest of params
RewriteCond %{QUERY_STRING} restrictBy\[foo\]=([^&]*)
RewriteRule (.*) $1?newfoo=%1 [L]
# Kinda works, but doesn't remove old params
RewriteCond %{QUERY_STRING} restrictBy\[foo\]=([^&]*)
RewriteRule (.*) $1?newfoo=%1 [QSA,L]
问题
我怎么能更换,而不会丢失数据,并没有额外的数据任何或所有3名PARAMS?从规则
感谢您的快速回复。我没有提到参数可能并不都是存在的,它们可能以不同的顺序存在。这仍然是你解决问题的方式吗? – nachito
那么'q = s'将永远在那里?你有很多参数,这些都是可选的? – anubhava
正确。 'q =东西'将永远在那里,其余的可选 – nachito