Exclude URLs with certain extensions from being redirected to index page

apache-2.2mod-rewrite

I have a set of rewrite rules that redirect all pages that don't exist to the index of my site:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Basically, the current rule is redirecting every URL that is not a directory or file to index.php, so www.example.com/thispageexists, www.example.com/thispagedoesntexist and also www.example.com/missingfile.xml go to the index.php page. If the URL doesn't exist in the CMS, that will trigger a 404 error back to the user.

I'd like to exclude any URL with certain file extensions, say .(js|css|ico|gif|jpg|png|xml|html|swf|ttf|eot)$, from being redirected, even if the file does not exist.

I still want to continue directing pages that don't exist to index.php but not if the url ends in any of those file extensions.

Best Answer

The rule is simple:

RewriteRule \.(js|css|ico|gif|jpg|png|xml|html|swf|ttf|eot)$ - [NC,L]

Just place it in a right place (as order of rules matters): for example, before this line: RewriteRule ^.*$ index.php [NC,L]

P.S. The [NC] flag is unnecessary for your rules with such (^.*$) pattern (it makes no difference at all).