RewriteCond: NoCase option for non-regex pattern ‘-f’ is not supported [htaccess]

After removing the .php extension from the URLs, I ended up seeing a warning “RewriteCond: NoCase option for non-regex pattern ‘-f’ is not supported and will be ignored” in the Apache error log. Below is the complete error message.

[Wed Nov 17 09:34:36.521614 2021] [rewrite:warn] [pid 9311] AH00665: RewriteCond: NoCase option for non-regex pattern '-f' is not supported and will be ignored. (/var/www/html/admin/.htaccess:11)
[Wed Nov 17 09:34:36.529046 2021] [rewrite:warn] [pid 9311] AH00665: RewriteCond: NoCase option for non-regex pattern '-d' is not supported and will be ignored. (/var/www/html/admin/.htaccess:12)

Here are the contents from the .htaccess:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

How to fix RewriteCond: NoCase option for non-regex pattern ‘-f’

By looking at the warning message, it is evident that it has arised out of one of the RewriteCond written in .htaccess file. From the Apache mod_rewrite documentation under RewriteCond directive, see the explanation for the ‘nocase|NC’ (no case) as read below:

This makes the test case-insensitive - differences between 'A-Z' and 'a-z' are ignored, both in the expanded TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. It has no effect on filesystem and subrequest checks.
CondPattern

CondPattern is usually a perl compatible regular expression,

The above sentence means the NC flag can be used for comparisons between TestString and CondPattern, but not on file systems. The parameters like ‘-d’ for the directory, ‘-f’ for a regular file, and ‘-l’ for the symbolic link are your CondPattern which are already non-case-sensitive. No point in explicitly mentioning again. So you can safely remove it for the rewrite condition for filesystem requests.

So, your new .htaccess shall be as below:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Note Missing NC

Note the missing [NC] at the end of the highlighted line.

That’s It!

Updated on November 17, 2021

Was this article helpful?

Related Articles

Leave a Comment