Top

Quick Tip: Setting a Fallback Image

June 11, 2008 by Susan Petracco 

No More Broken Images

Nothing looks less professional than an ecommerce site full of broken images! However, if you're running a site with a lot of products, it's bound to happen every now and then. Here's how to stop it from happening.

Most ecommerce sites use images to provide a visual representation of their product line. Product images are usually set in one of two ways - either specified in a database field provided by the shopping cart package, or specified in a template by using a file naming convention (for example, /images/PRODUCTCODE.jpg where PRODUCTCODE is replaced with the actual code for each product). Either is a convenient way for specifying product images. However, because the HTML code produced is always dependent on the existance of an image on the filesystem. there is a possibility that your site will display the dreaded "red X" or broken image icons - or in some browsers, such as Firefox, the image's alternate text.

Although it's always best to have images for every single product, it's also a good practice to configure fallback images. These are images that display whenever the specified image in the HTML output cannot be found on the server. We do this by using mod_rewrite rules in our .htaccess file.

A .htaccess file (note the dot in the front) is a method offered by Apache for setting up configuration parameters for the web site. We start by adding the following line to enable mod_rewrite if it does not already exist:

RewriteEngine On

The next set of code looks for any requested files that do not exist, and if the URL has the extension .jpg and resides in the images directory, it is replaced with the image "comingsoon.jpg" located in the images directory. The browser still sees the URL specified in the HTML, but the server delivers the comingsoon.jpg image instead:

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^images/.*\.jpg$ /images/comingsoon.jpg [L]

Of course, make sure you actually have an image named "comingsoon.jpg" in your /images/ directory!

This method will deliver a fallback image, one that might say "Photo Coming Soon", in place of any missing images. No more broken image links.

If your site uses multiple-sized images, such as thumbnails and full-sized images, you can have multiple copies of this code in your .htaccess file, with the appropriate changes to manipulate files in different directories or with different naming structures. Below is an example from one of our clients, whose site displays 4 sizes of images, with each size residing in a different directory:

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^graphics/40px/.*\.jpg$ /graphics/comingsoon/40.jpg [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^graphics/65px/.*\.jpg$ /graphics/comingsoon/65.jpg [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^graphics/100px/.*\.jpg$ /graphics/comingsoon/100.jpg [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^graphics/125px/.*\.jpg$ /graphics/comingsoon/125.jpg [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^graphics/275px/.*\.jpg$ /graphics/comingsoon/275.jpg [L]
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • Propeller
  • TwitThis

Comments

2 Responses to “Quick Tip: Setting a Fallback Image”

  1. Justin @ Palmer Web Marketing on July 28th, 2008 2:22 pm

    Great tip Susan, I’ve never considered this before.

    Do you know of a way to do this on a windows server?

  2. Susan Petracco on July 28th, 2008 2:56 pm

    Justin, it’s been awhile since I’ve worked with IIS, but I do remember playing around ISAPIRewrite (http://www.isapirewrite.com/) and IIS Rewrite (http://www.qwerksoft.com/products/iisrewrite/) before. This was back in 2003, so my memory is cloudy, but I think it worked pretty well.

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!





Bottom