Image 404-documents in .htaccess speeds up websites - ΩJr. Software Articles and Products

This information lives on a web page hosted at the following web address: 'https://omegajunior.globat.com/code/'.

Speed up your website by providing a simple error response for images, video, audio, etc. This article looks at how to do so in htaccess.

Specify the following in your .htaccess file:
<FilesMatch ".(aac|aif|aiff|flv|gif|ico|jpeg|jpg|m4a|mid|midi|mov|mp3|mp4|ogg|ogv|png|svg|swf|wav|webm)$">
ErrorDocument 404 "404 Not Found"
</FilesMatch>


We advise putting this after your regular ErrorDocument rule.

We have tested this and set this on our own website, the ΩJr.Net, and upon success we rolled it out to the other websites we administer.




Why This Works

If your CMS, templating system, or website is set to show a nice, helpfull error document whenever a web page is not found, chances are it is set too broadly, and will show the same error document for media resources like images, videos, and podcasts.

Many web browsers request the presence of all kinds of images (favicons and other bookmark images), and Microsoft Internet Explorer 11 and up requests a file called “browserconfig.xml”. If every visitor’s web browser asks for these files with every visit of every web page, and your web site delivers the error document for each not-found resource, the bandwidth your site uses and serves will add up quickly.

The more bandwidth your site uses for not-found resources, the less it can use for found ones. Therefore, limiting non-html media to simple 404 error messages will increase the speed of your website.

Also: serving small 404-not-found responses reduces the amount of time a web browser will wait for a response, speeding up how fast the browser can render the web page.


Prerequisites and Assumptions

* Your webserver runs Apache, NginX, or something similar that uses .htaccess files;
* You have access to the root folder of your web site;
* You are allowed to create / edit an .htaccess file;
* You understand a thing or 2 about .htaccess files;
* Your webserver is configured to read and apply the rules in .htaccess files (know that current versions of Apache have this disabled because of the performance impact on the servers).


Do Something Similar For MSIIS

We have tuned many Microsoft Internet Information Servers over the past decades. This speed-optimiser does not just work for Apache servers, but also for the MS IIS.

Unfortunately Microsoft’s webserver does not take .htaccess files. Instead, use its built-in control panel to supply specific error responses to non-html media files. Initially this does take a bit longer than with an .htaccess file, but once you find a good setting you can copy it from the resulting configuration file to reuse on other servers.

For not-found images we set the MSIIS to produce a small image containing the text “no such image”:



We love to hear from you!

Do you have any speed-improving tips for .htaccess files? Please let us know!

Need problem solving?

Talk to me. Let's meet for coffee or over lunch. Mail me at “omegajunior at protonmail dot com”.