检测 request_uri 中的双斜杠(空参数)

发布于 2024-10-09 03:54:37 字数 1472 浏览 0 评论 0原文

有关的 1 2 3

但是这个问题略有不同。

假设我有一个像这样的 RewriteRule:

RewriteRule ^test/([\w]*)/([\w]*)/([\w]*)/?$ go.php?q=THREE_$1_$2_$3

所以 http://localhost/test/a/b/c 被重写为 http://localhost/go.php?q=THREE_a_b_c

现在说我想要明确捕获中间参数被省略并产生双斜杠的情况,例如 http://localhost /test/a//c

所以我写了一个规则,例如:

RewriteRule ^test/([\w]*)//([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

但是可惜,该规则永远不会匹配,看起来 RewriteRule 看不到双斜杠,即使在 %{THE_REQUEST}%{REQUEST_URI} 中都保留了双斜线。

因此,我编写了如下规则:

RewriteCond %{REQUEST_URI} //
RewriteRule ^test/([\w]*)/([\w]*)/?$ go.php?q=TWO_$1_(NONE)_$2

有效,因为 RewriteCond 仅适用于 REQUEST_URI 中存在双斜杠的情况。但正如你所看到的,我仍然必须假设在使用正则表达式解析时没有双斜杠。

所有这一切都非常令人不安,因为我认为 RewriteRule 适用于 %{REQUEST_URI} 但事实证明它,它适用于其他东西(双斜杠剥离版本..?)

我的问题确实是,如果不是原始 %{REQUEST_URI} 那么 RewriteRule 依靠什么?

Related
1
2
3

But this question is slightly different.

Say I have a RewriteRule like:

RewriteRule ^test/([\w]*)/([\w]*)/([\w]*)/?$ go.php?q=THREE_$1_$2_$3

So http://localhost/test/a/b/c gets rewritten to http://localhost/go.php?q=THREE_a_b_c

Now say I want to explicitly catch the condition where the middle parameter is left out and a double slash results, like http://localhost/test/a//c

So I write a rule like:

RewriteRule ^test/([\w]*)//([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

BUT ALAS, the rule never matches, it seems RewriteRule does not see the double slash, even though the double slash is preserved in both %{THE_REQUEST} and %{REQUEST_URI}.

So I write a rule like:

RewriteCond %{REQUEST_URI} //
RewriteRule ^test/([\w]*)/([\w]*)/?$ go.php?q=TWO_$1_(NONE)_$2

And that works, because the RewriteCond only applies if there's a double slash in the REQUEST_URI. But as you can see I still have to assume there's NO double slash there when parsing with the regex.

All this is very disturbing because I thought RewriteRule worked on %{REQUEST_URI} but it turns out it doesn't, it works on something else (a double slash stripped version..?)

My question really is, if not raw %{REQUEST_URI} then what does RewriteRule feed on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-10-16 03:54:37

对此不确定,但您的第二条规则:

RewriteRule ^test/([\w]*)//([\w]*)/([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

似乎正在尝试匹配这样的 URL:

http://localhost/test/a//b/c

或这样:

http://localhost/test/a///c

如果您将第二条规则更改为:

RewriteRule ^test/([\w]*)//([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

它应该起作用。

编辑:

真正的答案!

Apache 删除空路径段

现在我知道问题不仅仅是一个不正确的正则表达式,我搜索了答案。

Not sure about this, but your second rule:

RewriteRule ^test/([\w]*)//([\w]*)/([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

appears to be trying to match a URL like this:

http://localhost/test/a//b/c

or like this:

http://localhost/test/a///c

If you change the second rule to:

RewriteRule ^test/([\w]*)//([\w]*)/?$ go.php?q=TWO_$1_(EMPTY)_$2

it should work.

Edit:

The real answer!

Apache removes empty path segments

Now I know the issue is not just an incorrect regex, I searched for the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文