Subscribe in a reader - Subscribe via Email - 1928 Subscribers and Counting!!!

World of information, opinions, cricket and technology.

How to avoid Hotlinking of file and images using .htaccess hack



Yesterday, i posted a .htaccess hack to Block Visitors from specific IP using .htaccess

Today, i am here again to share with you one more .htaccess hack.

What is HotLinking?

Avoid / Prevent Hotlinking of images or files (Credit http://www.conquerhosting.com)

Avoid / Prevent Hotlinking of images or files (Credit http://www.conquerhosting.com)

Inline linking (also known as hotlinking, leeching, piggy-backing, direct linking, offsite image grabs and bandwidth theft) is the use of a linked object, often an image, from one site into a web page belonging to a second site. The second site is said to have an inline link to the site where the object is located.


Recently, i noticed one blogger published a post in his blog, instead of uploading the image in his own site, he linked the image of my site. Every time somebody visits his post, the image will be downloaded from my site.

There are two problems

- Loss of bandwidth.
- Image Theft (you might have created the image after a lot of hardwork and wouldn’t like somebody using your image without your permission)

There is a way in which you can stop this. It can be done by adding few lines of code in .htaccess.

Prevent Hotlinking of file and images using .htaccess hack


I am assuming that you know about .htaccess file. It resides in the root directory of your website. The root directory could be /www/, /httpdocs/ or /public_html/ directories. Any of them depends on the hosting service.

Make sure you backup .htaccess file before you making any changes.

  1. Login to your hosting service site and open code>.htaccess file. Make sure you backup the file before making any changes.
  2. Add the following code in code>.htaccess file and save the file.
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://niharsworld.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://niharsworld.com$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.niharsworld.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.niharsworld.com$ [NC]
    RewriteRule .*.(htm|jpg|jpeg|bmp|png|gif)$ - [F,NC]
  3. From the above line, you can make out that i am blocking hotlinking of htm, jpg, jpeg, bmp, png, gif files.

    You may remove or add any file extension that you want to avoid HotLinked.

If you know any other way, please share via comments. Hope you learned something from this post!

Technorati Tags: ,




RSS Feed

1928 readers are already subscribed to this blog! Why don't you be one of them? Subscribe to this blog via your favorite RSS feed reader or by entering your email address on the form below:


You Should Also Check Out These Posts:


21 Responses to “How to avoid Hotlinking of file and images using .htaccess hack”

  1. Lyndi says:

    I dislike hot-linkers almost as much as I dislike spammers. This little hack should sort them out. Thanks for this.

    Lyndis last blog post..WordPress Upgrades

    • Nihar says:

      @Lyndi, Me too. There is one problem. If we are using google gears or importing posts along with images to our local wordpress. If this rule is there then we won’t get the images. For that we need to comment out that line temporarily.

  2. Gagan says:

    this one is very useful, cute little trick. will help me a lot.

    Gagans last blog post..TheLast84 : Latest Blogger Template From BeepTheGeek

  3. Atul says:

    very useful post I’ll soon implement it. Never even thought of this Thanks

    Atuls last blog post..Words Women Mostly Use

  4. This is awesome! Thank you for the hack, I’m going to try it right now

  5. Chetan says:

    Playing with .htaccess file is really risky task, but its very powerful file in hosting. Thanks for sharing this trick.and yes backup for that file is also important.

    BTW Nice theme dude. :)

    Chetans last blog post..Google Pagerank 5 for me : Google just updated pagerank

  6. Ruchi says:

    Thanks Nihar, I was looking for it that how to ban the hotlinking, I will give it a try.

  7. Ramakanth says:

    Thanks for sharing this trick,i was facing this problem those leechers are killing my bandwidth,that’s why currently I’m using free photo-sharing sites,anyway thanks for sharing.

    Ramakanths last blog post..First Google chrome extensions are available

  8. Laptops says:

    thanks for sharing this trick, i have an site from which i have noticed alot of hot linking and my bandwidth exceeds earlier because of that,Now this is going to save me,

  9. [...] the past, i have shared a trick – How to avoid hotlinking of images using .htaccess hack. This trick will help you avoid [...]

  10. ravi says:

    Thanks for sharing this trick,i was facing this problem those leechers are killing my bandwidth,that’s why currently I’m using free photo-sharing sites,anyway thanks for sharing.

Leave a Reply





CommentLuv Enabled