niina.amniisia » bits, pieces and photos from sydney, australia and elsewhere

htaccess hot linking blocker fix (Sunday January 8th, 2006)

category: tech and web drivel

Technical blurt time..

People have been having problems seeing the thumbnails on this site’s homepage and it’s been bugging me. Sometimes they see the images. Sometimes they don’t. Fiddling around today I managed to recreate the problem by:

1) clearing the browser cache
2) closing the browser
3) in a browser going to the URL without the “/” on the end of it eg. http://niina.amniisia.com

Resulting in broken images! If on the other hand I put the “/” on the end of the URL (eg. http://niina.amniisia.com/) then the images appeared.

When I moved hosts a while ago I read through their forums and found this piece of code for .htaccess to prevent hot linking* of images:

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

replacing “yourdomain.com” with my domain obviously.

It seems the problem was with the trailing “/” to “yourdomain.com/” – I removed it and the images seem to be working fine, other peoples web sites are still blocked from displaying my images, and all seems dandy. So the revised code, for anyone who’s interested is:

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

Obviously I could have this completely wrong and perhaps nothing’s working for anyone anymore so if you’re seeing broken images, please let me know.

* hot linking is when someone places an image on their web site that is actually stored and served from another web site. Hot linking is an annoyance because it uses up my bandwidth – if someone has permissions to use one of my images then they should save the image and uploaded it to their own web site (which is anyway what I tell those nice people who ask permission).

4 comments to “htaccess hot linking blocker fix”
scot said on January 8th, 2006 at 7:57 pm:

Hey niin, remember when you told me you sometimes have trouble following one of my socio-politico ideas, well, we are equal now. I have no idea what any of that means, I just hope I don’t contribute to any of the problems from the bits I could understand.

Anyway, Dandy’s rule, ok!

Scot

niina said on January 8th, 2006 at 8:35 pm:

don’t worry your pretty little head over my gibberish Scot.

Dan said on January 8th, 2006 at 8:41 pm:

Heh, makes sense to me and well done for fixing your little problem.

scot said on January 8th, 2006 at 11:03 pm:

Oh I won’t be, I just wrote a song which if i was recording would require a 16 track because I have four different layers of sound which is taking up more thinking time…..spector spector spector, w.r, spector…