How To Avoid Image Hotlinking & Bandwidth Theft In Your Website

hotlinkingImage Hotlinking is a common cause of increase in Bandwidth utilization of a website. Bandwidth theft or “hotlinking” is direct linking to a web site’s files (images, video, etc.). An example would be using an <img> tag to display a JPEG image you found on someone else’s web page so it will appear on your own site, eBay auction listing, weblog, forum message post, etc. Bandwidth is expensive so a webmaster has to manage it carefully. One of the way to optimize the bandwidth usage is by compressing the website output. Also there are times when people will hotlink to an image hosted on your server from their blog/website which results in increase in bandwidth. For example:
<img src="http://yourwebsite.com/blog/folder/someimage.jpg"></img>
Code language: HTML, XML (xml)
Thus, for every page request for that blog, a request will be generated for this image to your server. This will eat up the bandwidth and also may increase CPU time. So how to stop hotlinking? Well just add following code in your .htaccess file: Your site url is www.mysite.com. To stop hotlinking of your images from other sites and display a replacement image called nohotlink.jpg placed in your images directory, place this code in your .htaccess file:
#Stop Image Hotlinking RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !google. [NC] RewriteCond %{HTTP_REFERER} !search?q=cache [NC] RewriteCond %{HTTP_REFERER} !msn. [NC] RewriteCond %{HTTP_REFERER} !yahoo. [NC] RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpe [L]
Code language: HTML, XML (xml)
The first line of the above code begins the rewrite. The second line matches any requests from your own mysite.com url. The [NC] code means “No Case”, meaning match the url regardless of being in upper or lower case letters. The third line means allow empty referrals. Then we have few lines to Allow Search Engines like Google, Yahoo, MSN etc to crawl the images.The last line matches any files ending with the extension jpeg, jpg, gif, bmp, or png. This is then replaced by the nohotlink.jpe file in your images directory. This JPEG image is using the extension jpe instead of jpg to prevent blocking your own replacement image. Before uploading your .htaccess file to your server make sure that there is not one there already! Applications such as WordPress use their own .htaccess file when installed, if this is the case then download the existing .htaccess file, add your extra code and then upload it back to the server.
Get our Articles via Email. Enter your email address.

You may also like...

4 Comments

  1. Framco says:

    Hi,

    i would like to use your example of the .htaccess and I have one question.
    My images are in de directory e2/gal_1/images/, e2/gal_2/images/ and so on.

    Can I repeat the statement:
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ e2/gal_1 /images/nohotlink.jpe [L]
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ e2/gal_2 /images/nohotlink.jpe [L]
    ……
    en so on?

    Respect & regards

    Franco

  2. fdmovie says:

    This information I need, thank you

  3. Roy says:

    Dear Viral
    How do i prevent direct access to the Images and PDF documents
    Using Apache with Perl cgi
    Regards
    Roy

  4. php redirect to url says:

    h viralpateli,
    can you give info on above topic that if i want to allow hotlinking from certain ips or websites then what will be the code

Leave a Reply

Your email address will not be published. Required fields are marked *