Stop Image Hotlinking using .htaccess
Image hotlinking is when another web site links to your images (usually without your permission or knowledge). Most webmasters don’t appreciate others linking to their images as this uses up their bandwidth and can end up costing the webmaster quite some money.
Using htaccess a mod rewrites, you can:
- Ban all hotlinkers
- Ban one particular hotlinker
- Show a different image to one particular hotlinker
In your image directory, create a .htaccess file and add the following lines to it.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://zann-marketing.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.zann-marketing.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://216.239.*$ [NC]
ReWriteRule .*\.(gif|jpg|png)$ - [F]
The first line tells your Apache web server to turn on the rewrite engine.
The second line is a rewrite condition which says, “if the referer is not empty”.
The third line is a rewrite condition which says, “if the referer is not http://zann-marketing.com”.
The fourth line is a rewrite condition which says, “if the referer is not http://www.zann-marketing.com”. Note that the only difference between line 3 and 4 is the www.
The fifth line is an optional rewrite condition which says, “if the referer is not Google image servers”. We’ve used the IP address in this case to indicate which is Google’s image servers. This line may be removed if you wish to ban Google from indexing your images.
The last line is the actual rewrite rule which says, “if anyone (except for the above conditions) requests a gif file, jpg file or png file, then don’t show them any images”.
2. Ban one particular hotlinker
By modifying the above rewrite conditions and rules, you can ban just one hotlinker, instead of all hotlinkers. Again, you’ll need to create a .htaccess file in your image folder and insert the following code. In this example, we’ll assume that Zann Marketing (http://www.zann-marketing.com) is the hotlinker.
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://www.zann-marketing.com/.* [NC]
ReWriteRule .*\.(gif|jpg|png)$ - [F]
The first line turns Apache’s rewrite engine on.
The second line is a rewrite condition which says, “if the referer is http://www.zann-marketing.com/ or any of that web site’s pages”.
The third line is the rewrite rule which says, “if the above condition is filled, then don’t show any images”.
3. Show a different image to one particular hotlinker
A minor adjustment to the htaccess file code above will result in showing a different image to one particular hotlinker. You’ll need to create a .htaccess file in your image folder and a different image that you wish to use for this hotlinker, eg hotlinker.gif. Add the following code to your new .htaccess file. Again, we’ll use Zann Marketing (http://www.zann-marketing.com) as the example of the hotlinker.
RewriteEngine On
RewriteCond %{HTTP_referer} ^http://www.zann-marketing.com/.* [NC]
RewriteRule .*\.(gif|jpg|png)$ hotlinker.gif [L]
The first line tells Apache to turn on the rewrite engine.
The second line is a rewrite condition which says, “if referer is http://www.zann-marketing.com/ or any of it’s pages”.
The third line is the rewrite rule which says “when any image is required from this folder and the above rewrite condition is satisfied, then show the image hotlinker.gif”.

