如何使用转义查询字符串重定向完整的请求 URL



我有一个URL:http://example.org/abc?a=1&b=2(查询字符串是可变的)。

我想使用 mod_rewite 将其重定向到以下 URL:http://example.org/test.php?url=abc%3Fa%3D1%26b%3D2(查询字符串被转义)。

不希望网址变成:http://example.org/test.php?url=abc&a=1&b=2,

这是我使用时得到的:RewriteRule ^(abc) test.php?url=$1 [QSA].

我也试过:

RewriteCond %{THE_REQUEST} ^[A-Z]+ ([^s]+)
RewriteRule ^(abc) test.php?url=%1

但无济于事。有什么建议吗?

您需要为此使用B (escape backreferences)标志:

RewriteCond %{THE_REQUEST} s/+(S+)sHTTP [NC]
RewriteRule ^abc/?$ test.php?url=%1 [L,NC,B]

然后检查 $_SERVER["QUERY_STRING"] 的值,将显示为:

url=abc%3fa%3d1%26b%3d2

我是这样做的,类似于@anubhava:

RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(abc) test.php?url=$1%3F%1 [B]

$1 表示abc,%3F 表示?,%1 是查询字符串,全部使用 B 标志进行转义。

最新更新