Latest Posts »
Latest Comments »
Popular Posts »

.htaccess – How To Stop Hotlinking

Written by The Geekette on October 29, 2008 – 8:02 pm
Posted in Internet | 13 Comments »


Using an .htaccess file in the root of your site, you can stop others from hotlinking to your files. Whether it is images, .zip files or executables, others hotlinking to them is stealing your bandwidth. To prevent this, you can paste this into a file, upload it to the root of your website and then rename the file to .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?yourdomain.com [NC]
RewriteRule .(jpg|jpeg|png|gif|exe|zip)$ – [NC,F,L]

replace yourdomain.com with the name of your domain.com, yourdomain.net etc.

If you want to redirect the hotlinker to an image that shows you do not want others to hotlink to you – such as

 

No More Hotlinking!

No More Hotlinking!

Use the following statements in your .htaccess file but make sure you do not place the image on the site itself you do not want hotlinking from.

If you place the image on the domain that you are restricting hotlinking from, you will put the server into an endless loop. Use another domain to host this image from (as long it is one of your domains or you have permission to host the image there). You can use Photobucket, Image Shack, Picasa, Amazon Simple Storage Service and similar places where you can host images from – to host your no hotlinking image.

Next, use the following code in your .htaccess file 

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?yourdomain.com [NC]
RewriteRule .(jpg|jpeg|png|gif|exe|zip)$ http://www.anotherdomain.com/images/nohotlink.jpg [NC,R,L]

Replace yourdomain.com with the name of your domain.com, yourdomain.net etc. and replace http://www.anotherdomain.com/images/nohotlink.jpg with the path to another domain that hosts the image you want to use to show instead of the hotlinked image.

Afterwards, use the tools at http://www.htaccesstools.com/test-hotlink-protection/ to check to see if your file is hotlink protected. If you do not see your image after pasting the direct link to it, you know yours works!

Once again, I must reiterate:

Make sure you do not place the image on the site itself you do not want hotlinking from.

Popularity: 8% [?]

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.


§


Email This Post Email This Post | Print This Post Print This Post |



Tags: , , , , , , , , , ,
13 Comments »

13 Comments to “.htaccess – How To Stop Hotlinking”

  1. minanube (1 comments) Says:

    thank u for your helpful article

    i have try some trick but not working, now it’s working :D

  2. The Geekette (26 comments) Says:

    @minanube Thank you for your comment! I am very glad you found this article useful. I am also happy for you that it was able to help protect you from others doing the same to you (hotlinking) I wish there was an easier way.. I guess there are just so many that do not realize what they are doing when they DO hotlink!

  3. techniqueal t. (1 comments) Says:

    i didn’t know that the secret lies in htaccess. this is a fantastic tip! thanks so much for sharing ^_^

  4. The Geekette (26 comments) Says:

    @techniqueal t. I am glad it helped! I really have had a LOT of success with it. It is really saving me a lot ! Some of the images that were being hotlinked from my site were over 1 meg big. Though that might not sound like much.. when one 1MB image is displayed a million times a month.. that is a gig of bandwidth I have to pay for.. then multiple that times 10, 20 or even 50 images.. and you can see it uses a lot of bandwidth. And, most providers only allow X amount of bandwidth a month before the user starts paying extra money!

    On top of that, consider some companies limiting bandwidth every month. It seems to be a new trend. At the rate some people hotlink files.. they could use ALL my bandwidth in a month and I wouldnt even get to see the google homepage. Ok, so that is exaggerating.. it is not going to go THAT fast.. but it is the principle!

  5. AZL (1 comments) Says:

    Hi

    This is very useful. I have a spammer that has hotlinked to my Scritp in my CGI_BIN folder. What do I need to insert in the last line to stop a link to the script?

  6. Compute Live (1 comments) Says:

    What if I want to allow two domains to access images on my server…? Then what would be the code

  7. SlimJim (1 comments) Says:

    Ok, I just want to say thank you for posting this info.

    I have a blog that is with Blogger, in that blog is over 441 .zip files, I’m getting hotlinked like crazy.

    I plan on switching to Wordpress very soon, I’ll use this setup (thanks).

    When I setup my new Wordpress site, I’ll switch all of my old .zip files to include a single text file that will direct traffic to my new Wordpress site (LOL, that will fix them)! I’ll leave all my old images (hosted on Picasa) that they hotlinked to alone, so they won’t realize they are sending me their traffic, sorry If I sound harsh, but I’ve been racking my brain all day, looking for a way to stop the hotlinking.

    So by them hotlinking to me (like crazy) they will be giving my new Wordpress site a nice little boost of traffic, when I first startup.

    Again thank you for this post/info, you have gave me hope, & the nudge I’ve needed to actually move up to Wordpress.

    Thanks,

    PS. This does work with .zip files also, correct? Just want to double check before the Wordpress move. :)

  8. The Geekette (26 comments) Says:

    As long as you have zip in this last line
    RewriteRule \.(jpg|jpeg|png|gif|zip|exe|tgz)$ http://ericawashere.com/images/erica-says-no-hotlink-my-files.jpg [NC,R,L]

    this means it is denying hot linking to jpg jpeg png gif zip exe and tgz files

    http://ericawashere.com/images/erica-says-no-hotlink-my-files.jpg [NC,R,L] This part sends it to a jpg that I made

    if you want to see… http://ericawashere.com/images/erica-says-no-hotlink-my-files.jpg

  9. The Geekette (26 comments) Says:

    you can add as many domains you want

    for example this would be two

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?aleeya.net [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?ericastjohn.com [NC]
    RewriteRule \.(jpg|jpeg|png|gif|zip|exe|tgz)$ http://ericawashere.com/images/erica-says-no-hotlink-my-files.jpg [NC,R,L]

  10. The Geekette (26 comments) Says:

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?aleeya.net [NC]
    RewriteRule \.(jpg|jpeg|png|gif|zip|exe|tgz)$ http://ericawashere.com/images/erica-says-no-hotlink-my-files.jpg [NC,R,L]

    putting your domain where i have aleeya.net would cover all images including your cgi bin as long as the cgi bin is under that domain

  11. The htaccess Rules You Must Have On Your Blog | Blog Optimization Guide | Zemalf Says:

    [...] Source: http://www.girlgeekette.net/internet-info/htaccess-how-to-stop-hotlinking/ [...]

  12. Sofia (1 comments) Says:

    Thank you A MILLION times for posting this!
    It took me a bit of Google searcing to find your article, but it was definitely worth the search!
    I’ve read some similar posts but I really needed an example to understand it.
    Never tried editing my htaccess before, it’s been empty until now!

    Now I can prevent all those pesky bloggers from using my images! ;)

  13. Eien Chikara (1 comments) Says:

    If I can’t hotlink and image I would instead link to the site itself. But if everyone starts doing that won’t it take up a lot more bandwidth? Am I missing something here?

Leave a Comment

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Please understand with hundreds of SPAM comments, it takes a while to sort through to approve the good comments. Sometimes, by accident, some good comments accidentally get marked as spam. Please let us know if your comment did not get posted. Also, if your comment has a link to it or anything else in it that would make it seem like spam, it will be deleted. So please post a real comment and not one that is only made to link to a site only. Otherwise, it could get deleted.