HOWTO: Prevent website bandwidth theft with .htaccess

Some people can be so ignorant. Nothing aggravates me more than a forum user who hotlinks an image or file from your site on a popular forum, but when webmasters create file repositories, and all of the files are linked from other peoples servers. Without permission.

I’m sure there are many ways to prevent these people from harvesting your bandwidth, if you use apache with mod_rewrite, just by adding a couple lines of text to a file called .htaccess will do wonders.

What is Hotlinking?

Straight from Wikipedia:

Hotlinking aka. Inline linking is the placing 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. Inline linking is also known as leeching, direct linking or bandwidth theft.

Let’s start by opening up a text editor and creating a file called .htaccess In this file you want to put something like this:


RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?attackofthegamer.com/.*$ [NC]
RewriteRule \.(gz|zip|rar|gif|jpg|js|css)$ - [F]

Basically what this does is check if a request to any file with a .gz .zip .rar .gif .jpg .js .css extension comes from the referring URL http://www.attackofthegamer.com If the request comes from a referral that isn’t http://www.attackofthegamer.com it’s rejected. You will want to swap the domain for your own to make it work.

If you are just protecting images. You can mess with the culprit and change the image on them to something of your choice, with a comical outcome. Just create a .htaccess file that looks like this.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(png|gif|jpg)$ http://www.mydomain.com/badimage.gif [R,L]

Just change the domain name, and the url to the image you want to swap with and you are set. Once you have the .htaccess file created use your FTP program and upload it to each directory you want to protect.

Voila, you should now be protecting against bandwidth pirates. Happy Hunting.

2 Responses to “HOWTO: Prevent website bandwidth theft with .htaccess” »»

  1. Comment by kuriharu | 05/10/07 at 10:57 am

    I’m trying the last one but it doesn’t seem to work for me. I’ve swapped out the domain name with mine, and the graphic name (badimage.gif) with a graphic on my server. But when I right click and save graphics it still works. I’ve put the .htaccess file in both the root directory (where the .html page exists) and in the images directory where the images reside. No luck, I can still save images from my site.

    mods_rewrite is enabled.

  2. Comment by jason | 05/10/07 at 12:55 pm

    Kuriharu, it won’t prevent the user from downloading the image. It will prevent them from linking to it from another site. ex. <a href=”http://yourdomain.com/image.jpg”>

Leave a Reply »»

alternative to paypal alternative to ccbill verifiedpass

Warning: stristr() [function.stristr]: Empty delimiter. in /home/jason/attackofthegamer.com/htdocs/wp-content/plugins/wassup/wassup.php on line 2093