Prevent hot-linking

Hot-linking is the act of someone showing an image from your site on a site that ain’t yours, meaning that you get no visitors or any kind of recognition for having the image on your site, but you’ll still have to pay for it with your bandwidth. That sounds kind of unreasonable, don’t you think?

So let’s learn how to prevent it by adding a few rows in our .htaccess file in the root of our website.

Add the code in the box into your .htaccess file to make it work.

# Anti-hotlinking
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

The first row checks if the referer (i.e. the place on the Internet your visitor came from) isn’t your own domain.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]

The second row checks if the referer isn’t empty, i.e. the user typed in the URL to the image directly in her browser.

RewriteCond %{HTTP_REFERER} !^$

The third row generates a HTTP 403 Error (Forbidden) if the two above conditions were met and the requested file type was one of jpg, jpeg, gif, bmp or png.

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

So in short, this code denies access to your image if you aren’t requesting it from kerola.nu or directly through the browser window. Exactly what we wanted! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>