Disable Hotlinking To Prevent Other Sites from Stealing Your Bandwidth – .htaccess trick

When a site is using images hosting on your website, it uses resources from your server. This process is called Hotlinking.  In other words it is using an image from another site. For example, someone was reading one of your blog posts, and they liked the image in it and decided to publish it in their own blog, so instead of hosting the image on their own, they can just link from their pages to the images hosted on yours(serving image from your domain). Hot-linking can greatly affect the performance of your site and slow down your site, since resources from your server is used by many sites by hot-linking it, as a result your site may slow down and I hope you know slow sites are penalized by Google.

 

You can prevent other sites from using your documents or images or any other server resources, by disabling hot-linking from .htaccess. The .htaccess file in your root directory can be configured to prevent bandwidth steal and also help you increase security and stability, reduce bandwidth and increase usability. Hence saving your server resources and bandwidth. If you can’t find the .HTACCESS file on your host, then contact your web host on how to access your directories and configure it for you. So it is a good practice to prevent images hot-linking.

 

You must be very familiar with the below pictures. This is the example for a domain which has disabled hotlinking using .htaccess

Below is the .HTACCESS trick to prevent others from stealing your bandwidth:

 

To Disable hotlinking for all:

 

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteRule .*.(gif|jpg|png|ico)$ – [F,L]
</ifModule>

I the above example, make sure you replace “example.com” with your own domain name before adding it in .htaccess file.  You can also replace or edit file types mentioned on the“RewriteRule” line. But be sure to separate each file type with “|”.

 

Disable Hotlinking and Display error message

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteRule .*.(gif|jpg)$ http://www.example.com/hotlinkingisbad.gif [R,NC,L]
</ifModule>

In the above example, when someone tries to use your server resources, they will be served with an image filehotlinkingisbad.gif” (If it doesn’t exist you can make one). Again make sure you replace example.com with your own domain name.

 

Allow some domains from Hotlinking

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?gmail.com/.*$ [NC]
RewriteRule .*.(gif|jpg)$ http://www.example.com/nohotlink.gif [R,NC,L]
</ifModule>

In the above example, I’ve added Gmail.com in the line 5, allowing to hotlink resources (images etc) from your site. Anyone else would be served with the image “nohotlink.gif”.

 

 

To disable hotlinking specific domains but allow others

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.example.com/nohotlink.gif [L]

 

How to test image hotlinking:

To test your image if its still hotlinking, open this online html editor by Techgyo : Real-time HTML Editor and enter the image code:

<img src="your image url">