Custom HTTP 404 image vs. page on Apache

In most cases, server administrators define error pages in their apache configuration, but they often neglect to appropriately handle images, and other content types for that matter.

Generally, something similar to the following will be found in the Apache httpd.conf file:

ErrorDocument 404 /errors/404.html

You can use the file extension of the request to provide content that is at least “usable” to the browser, you could even provide a “cachable” response unless you expect to fix it quickly.

The advantage of this approach is that the HTTP Response code remains 404 but the user gets something other than a “broken” image on their screen. I generally make it a small 1×1 pixel gif.

An example for images would be…

<FilesMatch ".(jpg|png|gif|ico)$">
ErrorDocument 404 "/path/to/404.gif"

NOTE: you could use a similar mechanism for .css and .js files and provide cachable responses with comments within them to avoid the validation errors caused by 404’s.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.