Report inadequate content

Apache RewriteCond -f check file exists solution

{
}

Apache RewriteCond -f check file exists solution

If your Apache virtualhost or htaccess configuration uses a rewrite condition (RewriteCond) in order to allow nice URLs, you should be aware that since Apache 2.2 the "check if file or exists" works a little bit different. Any of the following examples might have stopped working for you:

   RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-s

The solution to correct it is very simple, but I couldn't see it documented. All you have to do is to add the variable DOCUMENT_ROOT before the REQUEST_FILENAME

The following example redirects all non existing files to index.php, here is the difference.

Before Apache 2.2:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.+) /index.php [QSA,L]

Apache 2.2 and later:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^/(.+) /index.php [QSA,L]

The Apache 2.2 documentation is here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

{
}
{
}

Comments Apache RewriteCond -f check file exists solution

Thank you!
Vladimir Vladimir 27/03/2014 at 09:50
Thank you! I've been trying to get these rewrites working for at least an hour now, and your example was the solution.
Nejra Nejra 02/04/2014 at 16:46

Leave your comment Apache RewriteCond -f check file exists solution

Log in to Obolog, or create your free blog if you are not registered yet.

User avatar Your name