问题描述
限时送ChatGPT账号..我使用 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]
当我访问 /search
(或 /search/
)时,我被正确重定向到 /search/?query=test
(根据最后一条规则)
When I visit /search
(or /search/
) I am correctly redirected to /search/?query=test
(as per the last rule)
从那里开始,RewriteCond
和 RewriteRule
应该启动并将我重定向到 /search/test
,对吗?据我了解,我的第一个 RewriteRule
中的 %1
对应于 RewriteCond
中的 (.*)
应该包含 test
.
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
.
然而,实际发生的是我被重定向到/search/test/?query=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?
然后我陷入了重定向到 /search/test?query=test
的无限循环中,因为第一个 RewriteCond
和 RewriteRule
一次又一次,一次又一次地踢......
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...
我做错了什么?!
谢谢!
推荐答案
您需要指定一个 替换中的空查询 以防止将原始请求的查询附加到新 URL:
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?(查询字符串追加)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论