Prevent hotlinking of your images using htaccess


If you suddenly experience a lot of traffic to your images that are not coming from your site, it’s often because of hotlinking. If you’re on limited bandwidth this can be a real pain. Here’s an easy way to prevent it using your .htaccess file. So here is how to do it in four lines:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ [NC,R,L]

If you want more sites to be able to implement your images, you can add more lines like this one:

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]

The image in RewriteRule \.(jpg|jpeg|png|gif)$ [NC,R,L] should not be a picture on your domain, as your blocking will block that too. And all kinds of bad stuff happens! Just don’t do it.

That’s all that you need to do, simple right? 🙂