2

I want to prevent Apache from logging "File does not exist" errors for certain irrelevant non-existing files that are often requested, such as "apple-touch-icon-120x120.png". These log entries clutter my log and make it difficult to see "real" problems. This is what I came up with:

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

The idea: If the file requested isn't a regular file and it isn't a symbolic link either, the RewriteRule will cause the error not to be logged. That kind of works.

Now I'm facing the following problem: If I actually create a symbolic link named "apple-touch-icon-120x120.png", then my RewriteRule still fires and I get 404. Without the rule or when I make the file a regular file and not a symbolic link, the file is served correctly. I thought that by using the !-l flag I could make my rule fire only if the requested file is not a regular file and it is not a symbolic link either. But that doesn't seem to work ...

I don't see anything suspicious in Apache's error log. What might I be doing wrong here?

David Scherfgen
  • 265
  • 2
  • 6
  • 2
    Where are you putting these directives? "prevent Apache from logging "File does not exist" **errors**" - Are you referring to the "error log", or the "access log"? – MrWhite Apr 19 '21 at 16:05
  • I had it in the vhost configuration, which was the problem. Thanks. – David Scherfgen Apr 19 '21 at 17:54

1 Answers1

5

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:


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.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • 1
    Thank you. I had that in the vhost configuration indeed. So inside a it works. Could I also use directly, or would that not work with non-existent files? And yes, I made a mistake when I said it works when I create a regular file with the name matched by the regex. – David Scherfgen Apr 19 '21 at 17:52
  • 1
    `` (and ``) only matches physical files, so it won't match if the file does not exist, as you suggest. Although there's rarely (if ever?) a need to put mod_rewrite directives inside a `` container - if that is what you are suggesting? `` containers are also processed later - changing the order of processing if that is a concern. – MrWhite Apr 19 '21 at 18:28
  • Which file is to be edited to change the log level? The link to the Apache Docs is not clear to me. Also, can I assume setting loglevel to warn will not suppress PHP Warnings such as "PHP Notice: Undefined variable:" and "PHP Notice: Undefined index:"? – Chiwda Jul 07 '22 at 04:17
  • @Chiwda "Which file" - That depends on your config. Wherever the config resides for the vHost you are changing. Setting `LogLevel` in Apache has nothing to do with PHPs error logging. – MrWhite Jul 07 '22 at 08:05
  • So I found which file: /etc/apache2/apache2.conf (may be useful for those on Debian & similar OSes). However, setting the loglevel to error results in all PHP messages disappearing also. So this is not the right solution for me. Any suggestions where to look? – Chiwda Jul 16 '22 at 08:44