i heart mod_rewrite
A success story in hotlinking hell.
The oasis in the desert that I found in the mod_rewrite land of Apache’s world is the delicious line of code below:
RewriteRule \.(jpe?g|gif|bmp|png|pdf|swf)$ - [F]
What does it do? Well, let’s Tarantino
it and start at the beginning even though you know the ending; then you’ll find out why the code above is deliciously, sumptiously, clandestinely my favorite.
So I downloaded the raw access log files from one of my subdomains (this blog) to review who’s accessing my site. Well, if anyone’s ever looked at one of these, it’s a pain in the butt. However, opening the text file in a spreadsheet software and saying “convert the text to columns” helps oh-so much.
So I’m clicking and reading through all the info and then I start to notice certain web sites accessing image files. Hmmm.. could they be hotlinking
those images? Let’s see… copy/paste URI into browser… and scroll down. View > Page Source… Ctrl + F to find zoblue click
I could have screamed when I saw the source code pointing to images on my blog. Well, I shed no tears for these thieves, these lowly creatures who feel entitled to steal a small gif icon I actually created in Fireworks with my own hands! I immediatley went back into my .htaccess
file–which I hadn’t touched in a year–and began to update it.
As it turns out, I was allowing access to certain files under Extensions to Allow in CPanel. Read about CPanel’s Hotlink Protection
to learn more. What I had to do was block access to certain files from web sites that were not my own.
So then I found out that I can add a simple line to my .htaccess file to forbid access to image files (or any kind of file). Adding the line below tells my server to forbid access to: jpeg, jpg, gif, bmp, pdf, and swf files.
RewriteRule \.(jpe?g|gif|bmp|png|pdf|swf)$ - [F]
But this will also stop your own web site from accessing these files, so let me put in all the code so I don’t get in trouble for giving incomplete code.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://my.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://my.website.com$ [NC]
RewriteRule \.(jpe?g|gif|bmp|png|pdf|swf)$ - [F]
Now, I’m not good on explaining the whole thing. So go read: preventing image hotlinking with .htaccess to get a nice clean explanation.
Happy preventing bandwidth theft and hotlinking! Use these handy mod_rewrite cheat sheets
to be cool.
