How to stop image Hotlinking with .htaccess

No Comments » Written on January 4th, 2013 by
Categories: Website Development

Hotlinking is a term used in the web community for those cases when a webmaster uses on his website an image that is hosted somewhere else. This does happen more and more, as there are many “lazy” webmasters that do not want to waste their time to at least copy the images they use on their own web server. Hotlinking simply puts extra processes on your hosting account, also using a good part of your website bandwidth, making it slower for your visitors.

Now that you have seen the downside of hotlinking, you might want to take the necessary steps to stop hotlinking from your website. The method presented below is for Apache / Linux servers, something most webmasters use to host their websites. So if you are having a Linux hosting account, go through bellow steps to forbid hotlinking.

      1. Create a text file on your computer, call it htaccess.txt

      2. Put one of the next codes in the file:

htaccess Version 1RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !www.example.com [NC]

RewriteRule \.(gif|jpg|png)$    -   [F,NC]

This code in htaccess Version 1 will simply stop any hotlinking for images with the extensions .gif, .jpg and .png. The www.example.com should be your website – this puts your website in the white list so that images will appear on this domain. If you will be using the same images on several other websites you own, you can simply add all domains to the white-list as showed below:

htaccess Version 1 – More whitelisted domainsRewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !www.example.com [NC]

RewriteCond %{HTTP_REFERER} !www.example2.com [NC]

RewriteCond %{HTTP_REFERER} !www.example3.com [NC]

RewriteRule \.(gif|jpg|png)$    -   [F,NC]

The code can be further customized not only to stop hotlinking, but to also show a different image instead of any the hotlinked images. In this case, I would show an image with the Logo of my website / text link to my website, to at least get some advertising out of that. For this case, use the following code for your htaccess file:

htaccess Version 2RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !www.example.com [NC]

RewriteRule \.(gif|jpg|png)$ http://www.example.com/image.gif   [R,NC]

In this above example, the www.example.com/image.gif should be the path to the image you want to push as a replacement for all hotlinking from un-approved domains.

      3. Once you have selected the code you want to use, save the file htaccess.txt and upload it to your website root folder. In the root folder, rename the file from htaccess.txt to .htaccess

This will tell the server what commands to execute and each time a hotlinking occurs.

You should know that this technique is not bullet-proof as it relies on the HTTP_REFERER variable, which is optional. But for most cases it will result in failed requests and it will help you reduce hotlinking and discourage other webmasters to use images hosted on your website, and therefore bandwidth theft.