重写规则。?仅适用于一个或多个字符?
在网上,我发现了几种定义 RewriteRule 的方法,例如:
RewriteRule .? 404.php [L]
RewriteRule . 404.php [L]
RewriteRule ^ 404.php [L]
但是为什么这些方法在输入垃圾 url 时会起作用?因为第一个 (.?)
意味着应该有零个或一个字符会导致 404.php(而不是一大堆字符或一个单词)。同样, .
和 ^
分别仅查找字符和行的开头(而不是包含几个字符或单词的行,对吧?)。这就是我根据 RegEx 帮助的想法。我错了吗?或者 ...?
On the net I found several ways of defining a RewriteRule like:
RewriteRule .? 404.php [L]
RewriteRule . 404.php [L]
RewriteRule ^ 404.php [L]
But why would these work when entering a garbage url? Because the first (.?)
means that there should be zero or one char which would lead to 404.php (not a whole bunch of chars or a word). Likewise, .
and ^
look only for a char and a start of a line (not a line with a few chars or words, right?), respectively. That's what I think based on the RegEx help. Am I wrong? Or ...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们之所以有效,是因为它们可能与“垃圾 URL”匹配。因为
.?
匹配零个或一个字符,所以.
匹配单个字符,而^
仅匹配字符串的开头(无论后面是什么) )。问题在于如何区分“垃圾”和“非垃圾”URL。
They work because they probably match that “garbage URL”. Because
.?
matches zero or one characters,.
matches a single character, and^
matches just the begin of a string (no matter what follows).The question is rather, how you differentiate between “garbage” and “non-garbage” URLs.
我还没有测试过,但我认为这绝对会将任何请求重定向到 404.php。
正则表达式
.?
将在 url 的任何位置搜索 0 或 1 个字符、任何字符。所以基本上,任何 url 都会匹配第一个正则表达式并重定向。行尾的
[L]
使 mod-rewrite 在找到匹配项时不再处理任何规则。I haven't tested, but I think this will redirect absolutely any request to 404.php.
The regex
.?
will search for 0 or 1 character, any character, at any position of the url. So basically, any url will match the first regex and will redirect.The
[L]
at the end of the line makes mod-rewrite to not process any more rules when it founds a match.