I recently went through my weblogs and noticed a bunch of leechers hotlinking to one of my sites (WiiLoveMario.com).  After some Googling, I found a little peice of code that will thwart all hotlinkers.  All I had to do was create a .htaccess file in the directory where the files were located and added the extensions of the files that I didn't want to be hotlinked.
 
Here's the code to add to your .htaccess file

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURSITENAME.com(/)?.*$ [NC]
RewriteRule .*\.(zip|mp3|xpi|pdf|abr|mov)$ http://YOURSITENAME.com/images/nohotlinking.gif [R,NC]

If you want certain file types to redirect to a specific page on your site, you can use the following code in your .htaccess:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURSITENAME.com(/)?.*$ [NC]
RewriteRule .*\.(pdf|abr|mov)$ http://www.YOURSITENAME.com/ [R,NC]
RewriteRule .*\.(mp3)$ http://www.YOURSITENAME.com/SOME-PAGE [R,NC]
RewriteRule .*\.(zip)$ http://www.YOURSITENAME.com/SOME-OTHER-PAGE [R,NC]

So all you need to do is:

  • replace the "YOURSITENAME.com" with your domain name.  
  • replace "zip|mp3|xpi|pdf|abr|mov" with whatever file extensions you don't want hotlinked (ie gif|jpg|jpeg|png).  In my case, I didn't want zip, mp3, xpi, pdf, abr and mov files
  • replace http://YOURSITENAME.com/images/nohotlinking.gif  with whatever file or page you want to redirect users to when they click on one of your hotlinked files

 

That's it.  Hope that helps you from leechers hotlinking your files.  Also, I noticed a little increase in traffic after putting the rewrite rule in place.  

I just tried it awhile

Anonymous on Thu, 03/04/2010 - 04:15

I just tried it awhile ago...These leechers has been pissing me off for a month now and never found away to get rid of it, hope this works on me. Thanks for sharing!

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5 + 9 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.