Are 404 images slowing down your WordPress?

The answer is yes, if you have a significant amount of 404 images on your  WordPress that are not found you could be seeing these effects.  Whenever an image is request and it’s not found it needs to run through the WordPress stack to generate the nice looking 404 page.

This post is going to show you a quick and easy way to modify your Apache htaccess file to speed up your busy website.

Don’t run WordPress for missing images

We want WordPress to not run it’s fancy 404 page for images that are not found, so we’re going to add the following rewrite rules to your htaccess.

Edit .htaccess inside your WordPress directory

Add the following lines the default BEGIN WordPress section

# BEGIN Faster 404 Images
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [nocase,redirect=404,last]
# END Faster 404 Images

You’ll place the above Faster 404 Images before the default WordPress rules which looks like below:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

That’s all you need to do and now your using Apache rewrite rules to check if the image exists, if it doesn’t exist it issues a 404 response and says this is the last response, and don’t continue any more rules.  So we’re then bypassing the rules to run WordPress.

I just implemented this on a customers website that had lots of missing images, they were running a load of 6+ and now it’s dropped down to 1.85.