Posts Tagged ‘hot-linking’

Prevent hot-linking using htaccess

Believe it or not, there are some webmasters who, rather than coming up with their own content will steal yours. Really! Even worse, they won’t even bother to copy to their own server to serve it up, they’ll just link to your content! no, it’s true, in fact, it used to be incredibly common. These days most people like to prevent this sort of thing, and .htaccess is one of the best ways to do it.

This is one of those directives where the mileage variables are at their limits, but something like this works fine for me..

how DARE they!
Options +FollowSymlinks
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com/ [NC]
RewriteRule .*\.(gif|jpg|png)$ http://mydomain.com/img/hotlink.png [NC]

You may see the last line broken into two, but it’s all one line (all the directives on this page are). Let’s have a wee look at what it does..

We begin by enabling the rewrite engine, as always.

The first RewriteCond line allows direct requests (not from other pages – an “empty referrer”) to pass unmolested. The next line means; if the browser did send a referrer header, and the word “mydomain.com” is not in the domain part of it, then DO rewrite this request.

The all-important final RewriteRule line instructs mod_rewrite to rewrite all matched requests (anything without “mydomain.com” in its referrer) asking for gifs, jpegs, or pngs, to an alternative image.