I recently discovered the concept of hotlinking, where other websites use images directly from your site without your
permission. This has several implications, the worst of which is that Google might give you a slap. It also steals your precious (or not so previous, depending on your hosting plan) and worse uses valuable server resources. So it’s a good idea to prevent this. I chose to prevent hotlinking using my .htaccess file. There are two ways in which you can achieve it, the first is simply to prevent images from being hotlinked (i.e the site which is hotlinking your images simply receives no image) or alternatively you could choose to display an alternative image of your choosing. I chose the first method for my sites, but I’ll cover the second for your information too.
For preventing hotlinking, use the following code:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|jpg|gif|png|bmp)$ - [F]
The above code simply prevents anyone from linking to any jpeg,jpg,gif,png and bmp type images on mysitecom (obviously replace mysite.com with your site name). I placed this code right after the PhpBay code required and also removed the “RewriteEngine On” line as this is already in place for the PhpBay code, but you’ll need it if it isn’t already there.
The second method is to replace the image that the stealing site see, using this code:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ http://mysite.files.wordpress.com/2007/09/no_hot_link.jpe [L]
The above code simply shows a no hot linking image of your choosing instead of the image requested. I create a blog on wordpress.com and uploaded an image to it and linked to this image instead of hosting the image on my site, as this defeats the point of preventing hotlinking.
There are other methods to prevent hotlinking (such as using php or Apache mod_rewrite) but I believe that this is one of the simplest methods of doing it.
Suggestions? Ideas? Feedback? Please leave a comment!
Related posts:






[...] Prevent hotlinking using .htaccess [...]