简体   繁体   English

Mod重写追加查询字符串参数,不替代

[英]Mod Rewrite Appending Query String Parameters, Not Replacing

I am trying to generate an external redirect that involves a few specific tasks. 我正在尝试生成涉及一些特定任务的外部重定向。

  1. If specific query string value is found in the URL, redirect. 如果在URL中找到特定的查询字符串值,请重定向。
  2. If redirected, replace one of the query string parameter names but not its value. 如果重定向,则替换查询字符串参数名称之一,而不替换其值。
  3. If #1 is false, then ignore rewrite and proceed 如果#1为假,则忽略重写并继续

Example: I have the url http://foobar.com/?a=123&b=456&c=blah 示例:我的网址为http://foobar.com/?a=123&b=456&c=blah

First, if parameter c = blah , redirect to http://barfoo.com/ 首先,如果参数c = blah ,则重定向到http://barfoo.com/

Second, replace a with x parameter so the final URL is http://barfoo.com/?x=123&b=456&c=blah 其次,用x参数替换a ,使最终URL为http://barfoo.com/?x=123&b=456&c=blah

Below is my best guess so far after researching on http://mod-rewrite-cheatsheet.com/ and Hidden features of mod_rewrite 在研究了http://mod-rewrite-cheatsheet.com/mod_rewrite的隐藏功能之后,这是我迄今为止的最佳猜测

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
RewriteCond %{QUERY_STRING} ^a=(.*)&b=(.*)&c=blah$ [NC]
RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,QSA,R=301]

However, the URL is appending the query string, not replacing. 但是,URL追加了查询字符串,而不是替换。

I get redirected to http://barfoo.com/?x=123&b=456&c=blah&a=123&b=456&c=blah 我被重定向到http://barfoo.com/?x=123&b=456&c=blah&a=123&b=456&c=blah

smacks forehead 打ack的额头

Removing QSA from the flags solved the issue. 从标志中删除QSA解决了该问题。 QSA means "append the existing query string to the current rewrite rule." QSA意思是“将现有查询字符串追加到当前重写规则中”。 It ignores whatever new query string parameters you are adding. 它忽略您要添加的任何新查询字符串参数。

I thought it was needed to have query string parameters exist for the rewrite rule itself. 我认为需要为重写规则本身提供查询字符串参数。

RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM