Apache: Default image if image is not found

The users uploaded images from my web site are kept in a separate folder. The problem is that the folder gets sometimes unsynced between the staging and the production server.

Therefore, i wanted to display a default (‘image not found’) image when one image is not found.

The sollution is NOT the ‘.httaccess’ file in that folder, but writing a mod_rewrite rule. The -f parameter means ‘if file exists’.

# When the image is not found, a default image is served
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/image_assets/(.*)(avatar|large|medium|thumbnail)(.{1,5})$ /var/mtc/default_images/$2.jpg

As you can see,  i have 4 types of images (and 4 default images, each with its own resolution).

You can test your regular expression here: http://jakarta.apache.org/regexp/applet.html.

P.S. : Do not forget to enable mod_rewrite 😉

RewriteEngine On

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: