Last Updated on April 14, 2021 by Amit

What is NC in mod-rewite?

How NC flag works in htaccess

NC stands for NoCase and is a URL rewriting flag used on Apache server.
We use NC to rewrite URLs in a case insensitive manner.
With NC flag set in RewriteRule you can use both upper-case and lower-case in URL path matching the rule’s pattern pattern.

If NC flag is present in RewriteRule then pattern of that rule can match URL path ignoring the character case.

How NC works in RewriteRule

Let’s see an htaccess example to better understand how this flag actually works :
Rule without NC

RewriteEngine on

RewriteRule ^foo$ /demo.php [L]

The above rule rewrites /foo to /foo.php but it will not work if you type /Foo because our URI has upper-case “F”. If you try a case-insensitive URI you will get a 404 not found error.

Rule with NC

RewriteEngine on

RewriteRule ^foo$ /demo.php [L]

The example above works for both types of URIs (upper-case and lower-case) .

When to use/not use NC flag in RewriteRule

If you are using a static pattern in RewriteRule then you need to use NC to accept any case characters in input URI.
In the following example NC is important :

RewriteRule ^File.php$ /file2.php [NC]

This example will also match /file.php because the NoCase flag is present there to avoid case-sensitivity.

You do not need to use NC flag when you are using a regex pattern that accepts any characters ie (.*)$ .

RewriteRule ^(.*)$ /file2.php

NC is not needed in the above example because the regex pattern matches all URI characters.

I Hope you enjoyed reading this Post. Thans for your time.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.