Loading

post

Fix Missing Images with a One Size Fits All Replacement

Near enough every website suffers missing images. They either show as blank sections within posts or they show in access logs as 404 errors, usually both. Sometimes they are caused by bad backlinks and other times by accidental directory deletion.

The good news is you can use .htaccess to replace missing images with a predefined stopgap until you find time to manually chase down the culprits and vanquish them by properly linking to them.

The bad news is that it’s a one size fits all solution: one image for all missing images or one image for all missing images of a particular file type. No matter how big or small the space for the missing image, the same replacement will show.

The Solution To Missing Image Files

Put this snippet into your .htaccess file to load a replacement image whenever an image file cannot be found:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule .* directory-path/replacement-image.jpg [L]

Use these rewrite rules when you know the images are missing and not just improperly referenced in backlinks by the addition of a trailing slash (as explained here).

The code works by getting Apache to confirm that a requested file doesn’t exist and if it doesn’t it tells Apache to confirm the file type against a specified set of file types then to serve a replacement if it is one of the specified type.

How it Works

Line by line:

RewriteCond %{REQUEST_FILENAME} !-f

This checks weather a file being requested exists. If it does exist, Apache skips this block of directives. If it doesn’t exist then it reads the next line.

RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$

This checks weather the file being requested (which doesn’t exist) is an image file of the type .gif, .jpg, .jpeg or .png. If it is then Apache proceeds to the next line otherwise it skips these directives.

RewriteRule .* directory-path/replacement-image.jpg [L]

This final line specifies a replacement for the requested image type. It tells Apache to  “rewrite whatever image file has been requested into this image file at..” wherever the directory path says it is, e.g “..directory-path/”. The [L] flag tells Apache that this line is the final one of this block.

You can put the replacement file wherever you want but always tell Apache where  it is relative to the .htaccess file that this snippet is in.

WordPress Users

There is a plugin called WP 404 Image Fix that achieves the same result. It is better to use .htaccess than a plugin. The more plugins you use the more security vulnerabilities you create but if you can’t use .htaccess files or mod_rewrite is disabled then the plugin will do the trick.

Over to You

How do you resolve 404s created by missing images? Give us a few tips so we can all benefit from your knowledge.


  • Pingback: Links 15/11/2011: Linux still Rules HPC | Techrights

  • Stefan Lisowski

    This is exactly what I wanted, though because I don’t do rewrites much, and don’t recall ever doing them from .htaccess files before, it took me a bit of googling before I discovered that the first line in the .htaccess file needs to be “RewriteEngine on”. At least — when I put it in there, everything worked. (Well after I also put “AllowOverride FileInfo” in httpd.conf for the relevant docroot so the .htacess would take effect…). Thank you very much.

    • http://journalxtra.com/ Dion de Ville

      You are very welcome, Stefan. I’m glad my ramblings helped ;)

      Most servers already have rewrites enabled. The best place to add the “RewriteEngine on” directive is in the server space’s root directory (above public_html or its equivalent).

All original content on these pages is fingerprinted and certified by Digiprove