背景
我想在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
问题
RewriteRule does not look at the query string的第一个参数,所以我不能替换单个参数,同时保持其余的url完好无损。此外,NONE,SOME或所有参数可能存在的事实,并且在任何订单中都会引发我的循环。
# 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个参数?
答案 0 :(得分:0)
从规则中删除QSA
以覆盖现有查询字符串:
RewriteCond ::%{QUERY_STRING} ::(?:|.*&)restrictBy\[foo\]=([^&]*)
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)restrictBy\[baz\]=([^&]*)
RewriteCond %1&%2::%{QUERY_STRING} ([^&]*)&(.*?)::(?:|.*&)q=([^&]*)
RewriteRule ^(.*)$ $1?q=%3&newfoo=%1&bazinga=%2 [L,R]