开发者

Detecting double slash (empty parameter) in request_uri

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 dou开发者_如何学JAVAble slash stripped version..?)

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜