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.”