本文介绍了我如何关闭QSA? (查询字符串追加)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Apache2和mod_rewrite的隐藏我的查询字符串。这些有问题的规则。

I'm using Apache2 and mod_rewrite to hide my query strings. These are the rules in question.

RewriteCond %{QUERY_STRING}                 ^query=(.*)$
RewriteRule (.*)                            /search/%1             [R=301,L]

RewriteRule ^search\/?$                     /search/?query=test    [R=301,L]

当我访问 /搜索(或 /搜索/ )我正确地重定向到 /搜索/?查询=测试(根据最后一条规则)

When I visit /search (or /search/) I am correctly redirected to /search/?query=test (as per the last rule)

从那里,的RewriteCond 重写规则要踢,重定向我 /搜索/测试,对不对?从我了解的%1 在我的第一个重写规则对应(。*) 的RewriteCond 应包含测试

From there, the RewriteCond and RewriteRule should kick in and redirect me to /search/test, right? From what I understand the %1 in my first RewriteRule corresponds to the (.*) in the RewriteCond which should contain test.

然而,实际发生的事情是,我重定向到 /搜索/测试/?查询=测试。因此,规则的作用,但由于某些原因查询字符串追加。 是它在某种程度上被自动添加QSA选项/地方?

However, what actually happens is I'm redirected to /search/test/?query=test. So, the rule works, but for some reason the query string appended. Is it the QSA option being automatically added somehow/somewhere?

我再停留在重定向到 /搜索/测试?查询=测试,因为第一个的RewriteCond 重写规则又一次,又一次...

I'm then stuck in an infinite loop of redirecting to /search/test?query=test because the first RewriteCond and RewriteRule kick in again, and again, and again...

我是什么做错了吗?!

谢谢!

推荐答案

您需要指定一个在替代以prevent原请求的查询空查询附加到新的网址:

You need to specify an empty query in the substitution to prevent the original requested query to be appended to the new URL:

修改查询字符串

在默认情况下,查询字符串保持不变地传送。你可以,但是,创建包含查询字符串的一部分替换字符串的URL。只需使用一个问号替换字符串内,表明以下文字应重新注入到查询字符串。如果要删除现有的查询字符串,结束与只是一个问号替换字符串。要结合新老查询字符串,使用[QSA]标志。

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

所以:

RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule (.*) /search/%1? [R=301,L]

这篇关于我如何关闭QSA? (查询字符串追加)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!