Hotlinking, Bandwidth Theft and mod_rewrite

Have you seen your bandwidth increase without reason !?!?!
I guess you have…
Hotlinking (Bandwidth Theft) makes your bandwidth limit up!

Senario
You have taken a few nice photos with your brand new camera and you have uploaded them to your blog (website) so family,friends and visitors could see them.
That is so… nice.
That is what the HotLinkers said too.

Solution
Use the mod_rewrite module of Apache.
Let’s say your site’s url is www.MyDomain.com.
Fire up your preffered editor, copy these lines below…

RewriteEngine On
# Uncomment the next line if you have placed your images in a folder called photos. As you can see /photos/ is in root directory and the .htaccess file will take care all the subfolders if you have any...
# RewriteBase /photos/
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?MyDomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png|JPG)$ http://www.MyDomain.com [R,NC]

and paste them to a file you will call .htaccess (mind the dot please… it’s important!)
Put the .htaccess file to the folder where your photos are…
That’s all!

Keep in mind that even Google will not be able to HotLink your images. If you want to allow this, just insert this line…

RewriteCond %{HTTP_REFERER} !^http://(www\.)?images.google.com(/)?.*$ [NC]

Before the RewriteRule.
Now you are done.

“The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.”
— Brian Behlendorf

“Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.”
— Brian Moore