Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite RewriteCond - is NC flag necessary for just domain part? And some more

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?

like image 787
Marco Demaio Avatar asked Apr 07 '11 13:04

Marco Demaio


People also ask

What is NC in RewriteCond?

The [NC] specifies that the http host is case insensitive. The escapes the "." - because this is a special character (normally, the dot (.) means that one character is unspecified). The final line describes the action that should be executed: RewriteRule ^(.*)$ http://www. example.com/$1 [L,R=301]

What is RewriteCond in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is NC in Apache redirect?

NC|nocase. Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI. In the example below, any request for an image file will be proxied to your dedicated image server.

What is mod_rewrite used for?

mod_rewrite lets you create all sorts of rules for manipulating URLs. For example, you can insert values pulled from the requested URL into the new URL, letting you rewrite URLs dynamically.


2 Answers

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].

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

like image 157
anubhava Avatar answered Sep 28 '22 00:09

anubhava


NC = nocase means regardless of the case of the incoming referrer traffic match with the given pattern.

See Apache [NC|nocase] flag

like image 45
Yarob Al-Taay Avatar answered Sep 28 '22 01:09

Yarob Al-Taay