Iowa
Iowa

Reputation: 2481

apache: redirect only 404 error messages to a different log file in httpd.conf

After a bit of searching, I couldn't find a way to redirect only 404 specific error messages to a different log file(not error_log). Please let me know if there is way to achieve this. Quick help would be appreciated. Thanks in advance. :)

Upvotes: 3

Views: 1882

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

The CustomLog directive has an env= parameter that you can add to the end. You can then set that env variable using mod_rewrite. So in your server/vhost config, you'd have something like this:

RewriteEngine On

# exclude directory index
RewriteCond %{REQUEST_URI} !/index\.(php|html?)$
# exlcude auto-index of directories
RewriteCond %{REQUEST_URI} !/$
# if the request is not a file, directory, or symlink
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# set the "notfound" environment variable
RewriteRule ^ - [E=notfound:true,L]

CustomLog /var/log/apache2/404-requests.log common env=notfound
CustomLog /var/log/apache2/requests.log common env=!notfound

So the custom log file /var/log/apache2/404-requests.log only gets logged to when the environment variable notfound is set, and the log file /var/log/apache2/requests.log only gets logged to for everything else. The auto indexing and directory indexing is a little hokey so that's why you need to exclude those from the notfound set of conditions.

Upvotes: 3

Related Questions