Disclosure: Hosting Canada is community-supported. We may earn a commission when you make a purchase through one of our links. Learn more.

Hotlink prevention techniques

Stopping other websites from showing your content or files is referred to as hotlink prevention. Commonly it’s used to stop other websites from showing your image content but it can also be used to stop the ones using your CSS or Javascript files. But the problem of hotlinking technique is that it consume your bandwidth that costs you money, so hotlinking technique is often termed as ‘bandwidth theft’.

.htaccess can be used to stop other websites from using or sourcing your web content, and it can even in turn show different content. For example, it’s very common to show what’s referred to as ‘angry man’ image in place of the originally desired image.

Note here that this functionality needs that you enable the ‘mod_rewrite’ on your web server. Because of the demands on the system resources, it’s not common that it’s already enabled so assure checking with your hosting service provider or system administrator.

Make a .htaccess file and follow the guidance and main instructions along with the text below to set-up hotlinking to prevent ‘.jpg’, ‘.css’ and ‘.gif’ files.

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

The code above will instruct Apache Web Server for blocking links to ‘.jpg’, ‘.css’ and ‘.gif’ files that aren’t coming from ‘http://www.yourdomainname.com/’. Ensure that you replace the ‘yourdomainname.com’ with your original website address before uploading the .htaccess file.

For setting up hotlink prevention for ‘.jpg’, ‘.gif’ files that show alternate content like an angry man image, you need to create an .htaccess file and follow the guidance and the main instructions in addition to the text below.

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

The code above will instruct the Apache Web Server to block the links to ‘.jpg’ and ‘.gif’ files that are not from ‘http://www.yourdomainname.com/’ and instead the file that’s to be shown is ‘http://www.yourdomainname.com/hotlink.jpg’. Make sure to change ‘yourdomainname.com’ with your appropriate website address before you upload the .htaccess file.

For additional information on hosting providers please check out our updated guide to Canadian hosting. We have also published detailed reports on Canadian VPNs including logging and pricing.