Preventing Image Bandwidth Theft Using Apache

I recently noticed this site’s images were getting a lot of hits – only the images, not the pages they were posted to. I discovered a lot of users of forums and chats linking directly to the images on this site, instead of copying the image and hosting it on their own website. This type of image linking is a drain on my poor server, and it’s considered bandwidth theft.

Here’s the code in my Apache httpd.conf file to prevent websites from “stealing” my images:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?steelwhitetable\.org [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?cairns.servehttp\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?bloglines\.com [NC]
RewriteRule .(gif|jpg|png)$ - [F]

This prevents .gif, .jpg., and .png files from being displayed on any website except those specified. So, if I wanted to allow google.com to display my images, I would add the following to the above:

RewriteCond %{HTTP_REFERER} !^http://(www.)?google/.*$ [NC]

You could even redirect those attempting to use a direct link to an image on your website to a different image. Add the following before the RewriteRule, where “bad.jpg” is the image that will be displayed on other websites:

RewriteCond %{REQUEST_URI} !^/bad.jpg$ [NC]