Jan 10 2008

Stop Hotlinking to Certain Images

While working on another site, I ran into an issue I needed to head off before I went live. I needed a way to stop people from hotlinking to certain files, while letting other images get hotlinked. My main goal was to allow people to hotlink to thumbnail images, but not the original image.

Here’s what I’ve come up with

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$ [NC]
RewriteCond %{REQUEST_FILENAME} !.*_thumb.(gif|GIF|jpg|JPG|jpeg|JPEG)$ [NC]
RewriteRule .*\.(gif|GIF|jpg|JPG|jpeg|JPEG)$ - [F]

You can change the _thumb part to what ever you use that is unique to your images. Also, you can add as many file types you need by adding a pipe “|”. The first few rewrite conditions are needed, or your own website won’t be able to display your images.

When I get a chance, I’ll update this to display a leeching hotlink image instead of a blank image.

TAGS:

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog