mod_rewrite RewriteCond - 仅域部分需要 NC 标志吗?还有更多
我在 htaccess
中多次看到这些类型的规则:
RewriteCond %{HTTP_REFERER} !^http://www.domain.it$ [NC]
或者
RewriteCond %{HTTP_HOST} !^www\.domain\.it$ [NC]
为什么在仅检查域部分时需要 NC
标志?
我注意到浏览器总是将域名中的大写字母转换为小写字母,因此我不明白 [NC]
标志在这种情况下有什么用处。
我的意思是,如果我们检查 url 的其余部分,我就明白需要 [NC]
标志,因为在 Unix 系统上 www.domain.com/index.html
是不同的文件比 www.domain.com/INDEX.HTML
但我不明白当我们仅检查 RewriteRule 中的域部分时需要 NC
标志。
既然您花时间阅读了上面的内容,让我再问一个小问题,与 NC
标志不直接相关,但仍然与 RewriteCond
有关
上面显示的两个 RewriteCond
都工作得很好,我认为只有点前有斜杠的才可以工作(!^www\.domain\.it$
),因为 点没有斜杠应该表示正则表达式中的'任何字符',而\.
表示点字符,但令人惊讶的是另一个也很好用,你知道吗为什么?
I have seen many times in htaccess
these type of rules :
RewriteCond %{HTTP_REFERER} !^http://www.domain.it$ [NC]
or
RewriteCond %{HTTP_HOST} !^www\.domain\.it$ [NC]
Why is the NC
flag necessary, when checking only the domain part?
I noticed browsers always converts uppercases in domain names into lower cases, so I don't see what the [NC]
flag is usueful for in this case.
I mean if we check the remaining part of the url I understand the need for [NC]
flag cause on Unix systems www.domain.com/index.html
is different file than www.domain.com/INDEX.HTML
but I don't understand the need of NC
flag when we check only the domain part in the RewriteRule.
Since you took the time to read the above, let me ask also one minor question not directly related to NC
flag, but still related to RewriteCond
Both RewriteCond
shown above work well, I thought only the one with slash before dots would work (!^www\.domain\.it$
) because the dot without a slash should mean 'any char' in regexp whilest the \.
means the dot char, but surpraisingly also the other one works well, do you know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
拥有
[NC]
绝对不是强制性的,但建议匹配域时拥有它。现代浏览器可能会将域名转换为小写,但是旧的浏览器和命令行实用程序(例如wget
、curl
等)又如何,因此您不应始终依赖客户端向您发送小写域名命名并保留[NC]
。关于你的第二个问题
.
匹配任何字符,因此它能够匹配www.domain.com
但它也会匹配www-您不想匹配的domain-com
文本。所以最好有www\.domain\.com
Having
[NC]
is definitely not mandatory but it is recommended to have it for matching domains. Modern browsers might be converting domain names to lowercase but what about old browsers and command line utils likewget
,curl
etc, so you should not always rely on clients sending you lowercase domain name and keep[NC]
.About your 2nd question
.
matches any character therefore it is able to matchwww.domain.com
but it will also matchwww-domain-com
text which you don't want to match. So it is better to havewww\.domain\.com
NC = nocase
表示无论传入引荐来源网址流量与给定模式匹配的情况如何。请参阅 Apache [NC|nocase] 标志
NC = nocase
means regardless of the case of the incoming referrer traffic match with the given pattern.See Apache [NC|nocase] flag
我不确定 [NC],但我想在某些不太可能发生的情况下,ENV 变量。可以包含大写字符。
正则表达式中的点表示一个字符,任何字符。因此,如果字符串是 http://www_domain-it 或 http://wwwadomain1it
I don't know the [NC] for sure, but I suppose that in some unlikely event the the ENV var. could contain upper case chars.
A dot in a regular expression means one character, any character. SO the first condition would also match if the string is http://www_domain-it or http://wwwadomain1it