mod_rewrite RewriteCond - 仅域部分需要 NC 标志吗?还有一些 [英] mod_rewrite RewriteCond - is NC flag necessary for just domain part? And some more
问题描述
我在 htaccess
中多次看到这些类型的规则:
I have seen many times in htaccess
these type of rules :
RewriteCond %{HTTP_REFERER} !^http://www.domain.it$ [NC]
或
RewriteCond %{HTTP_HOST} !^www.domain.it$ [NC]
为什么在只检查域部分时需要 NC
标志?
Why is the NC
flag necessary, when checking only the domain part?
我注意到浏览器总是将域名中的大写字母转换为小写字母,所以我看不出在这种情况下 [NC]
标志有什么用.
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.
我的意思是,如果我们检查 url 的其余部分,我就会理解在 Unix 系统上需要 [NC]
标志的原因 www.domain.com/index.html
是与 www.domain.com/INDEX.HTML
不同的文件,但是当我们只检查 RewriteRule 中的域部分时,我不明白需要 NC
标志.
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.
既然你花时间阅读了上面的内容,让我再问一个小问题与NC
标志没有直接关系,但仍然与RewriteCond代码>
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
上面显示的两个 RewriteCond
都可以很好地工作,我认为只有在点之前带有斜线的那个才可以工作(!^www.domain.it$
),因为 没有斜线的点应该在正则表达式中表示 'any char' 而 .
表示点字符,但令人惊讶的是,另一个也可以很好地工作,你知道为什么吗?
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?
推荐答案
拥有 [NC]
绝对不是强制性的,但建议拥有它以匹配域.现代浏览器可能会将域名转换为小写,但是旧浏览器和命令行实用程序(例如 wget
、curl
等)呢,因此您不应总是依赖客户端向您发送小写域名命名并保留[NC]
.
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 like wget
, curl
etc, so you should not always rely on clients sending you lowercase domain name and keep [NC]
.
关于您的第二个问题 .
匹配 任何字符 因此它能够匹配 www.domain.com
但它也会匹配 www-domain-com
您不想匹配的文本.所以最好有 www.domain.com
About your 2nd question .
matches any character therefore it is able to match www.domain.com
but it will also match www-domain-com
text which you don't want to match. So it is better to have www.domain.com
这篇关于mod_rewrite RewriteCond - 仅域部分需要 NC 标志吗?还有一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!