Apache2 RewriteCond %{REQUEST_FILENAME} !-f not working

I was having an issue with my Apache2 config not matching files with:

RewriteCond %{REQUEST_FILENAME} !-f

I didn’t really get to the bottom of the problem, but I found that prefixing with the document root solved my issue:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

I have no idea why Apache wasn’t resolving %{REQUEST_FILENAME} relative to the project root, but I have a solution, so I have moved on.

Apache2 REQUEST_FILENAME requires DOCUMENT_ROOT

I had a problem with my rewrite rules, that looked like this:

  DocumentRoot /var/www/trust.jj5.net
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) http://trust.jj5.net/sorry.html [L,R]

I was trying to redirect any request which didn’t match a file or directory. The !-f and !-d requirements were failing because the path to the REQUEST_FILENAME wasn’t fully qualified. I fixed the problem by including the DOCUMENT_ROOT:

  DocumentRoot /var/www/trust.jj5.net
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
  RewriteRule (.*) http://trust.jj5.net/sorry.html [L,R]

Happy days! :)