检测 request_uri 中的双斜杠(空参数)
但是这个问题略有不同。
假设我有一个像这样的 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 依靠什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对此不确定,但您的第二条规则:
似乎正在尝试匹配这样的 URL:
或这样:
如果您将第二条规则更改为:
它应该起作用。
编辑:
真正的答案!
现在我知道问题不仅仅是一个不正确的正则表达式,我搜索了答案。
Not sure about this, but your second rule:
appears to be trying to match a URL like this:
or like this:
If you change the second rule to:
it should work.
Edit:
The real answer!
Now I know the issue is not just an incorrect regex, I searched for the answer.