Apache: Don't log errors for certain non-existing files (regular file or symbolic link)

If you are getting "File does not exist" messages in your "error log" then the LogLevel (as set in the server config) is arguably set "too high". Certainly, too high for a production server. "File does not exist" is an info message, so you may have LogLevel info (or above) set in the server config. The default is LogLevel warn.

A 404 (file not found) is a normal HTTP response, it's not a "server error". So, ordinarily, this should only appear in the "access log", not the server's "error log".

The following levels are available, in order of decreasing significance (increasing verbosity):

emerg
alert
crit
error
warn    (default)
notice
info    "File does not exist" messages
debug
trace1
trace2
:

Reference:

  • https://httpd.apache.org/docs/2.4/mod/core.html#loglevel

RewriteCond %{REQUEST_URI} ^/apple-touch-icon(-\d+x\d+)?(-precomposed)?\.png$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [redirect=404]

Yes, if used in a directory context, this prevents the "File does not exist" message in the "error log" if the request does not map to a file or symbolic link. However, this is not the correct way to resolve this IMO. Use the LogLevel directive as described above.

If you are using this in a server or virtualhost context then it will unconditionally serve a 404 (which may be what you are experiencing? Although you say it works for "regular files"?) since the request has not yet been mapped to the file system at this stage, so REQUEST_FILENAME is still the URL-path (so the conditions !-f and !-l are always successful). In this context you need to use a lookahead instead, ie. %{LA-U:REQUEST_FILENAME}.

You also need to ensure that Options +FollowSymLinks is set.