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