The Cost of Hot-Linking
It had never really occurred to me before, because I hadn't ever had trouble, but I was utterly shocked when I got my quarterly statement through from my web-space host. I was surprised as my bandwidth seemed to have been hovering around 800Mb per day (which seemed a lot) but on the 13th March had shot up to 1200Mb a day! This started costing me money. At first about £6 a day, and the bandwidth just went up and up, until it reached about 1600Mb a day yesterday costing me £9 that day alone!
Of course, the moment I realised I went on a mission to find out what the hell was going on. The statistics the web-host provided seemed to show nothing unusual. I run about 10 sites on my web-hosting and none of them seemed to be getting lots of hits (according to the generated statistics).
So, I downloaded the original web-access logs that the web-servers churn out day-in-day out. Opening them in Wordpad and I noticed an unusual number of hits to a photo that was on my personal site from me and my wife's honeymoon. Huh? In the first 100 lines, at least 80 were to this image, all HTTP 200 (i.e. transaction completed ok).
I downloaded a great program called Web Expert Lite to analyse the logs more effectively, and there I spotted it.
The image was being hit over 13,000 times a day (20,000 on one day) to be used for people's profile pages on myspace.com, a social networking website. It had been added to HotFreeLayouts.com by some guy called Jesse, and apparantly (according to the page about it), has been used on 24,728 myspace profiles.
Now, I'm half flattered that one of my wife's photos should be so popular, however, I'm not prepared to pay £10 a day for some spotty teen to have it as their website's backdrop. I mean, it's not even nicely done - just look at the background on that Jesse bloke's website!
So, the way you stop this 'hot linking' as it's known it to re-write the .htaccess file like so:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?altlab\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]
Adding this to your .htaccess at the root of your domain will stop hot-linking from the listed addresses. Each line is a regular expression pattern match in the HTTP_REFERER. NC means ignore case when matching, and OR provides a logical OR with the next line. The last HTTP_REFERER pattern therefore doesn't have the OR.
If one of these match the last line matches an image file and the "- [F]" causes an HTTP 403 - Forbidden to be returned, blocking access to the image.
You might read this as:
if( myspace.com, blogspot.com, atlab.com OR livejournal.com ) then
if( jpg, jpeg, gif, bmp OR png ) then
return 403 Forbidden
endif
endif
I found this information on the AltLab htaccess tutorial and that page allows you to test whether your htaccess is working (hence why altlab appears in one of the patterns!).
So, the upshot is that no-longer can people hot-link to my images and cost me lots of money in excess bandwidth costs!! The second upshot is that 24,728 myspace profile pages now look blank. Ha!
The moral of the story is that you should download a local copy and not hot-link.
