How to setup Hotlink Protection in Linux

Hotlink Protection allows you to prevent other websites from directly linking to files from your website. This means that when another website is visited, it cannot load pictures, css or javascript from your pages – this is one of the ways for limiting the outbound traffic for your account.

An example of hotlinking is for a different website using HTML code to display an image from your site. In effect, the other website is using up your bandwidth since every time the picture is requested, the visitor’s browser is fetching it from your server.

To enable hotlink protection for the desired file types you should add the following lines in the .htaccess file for your website:

RewriteCond %{HTTP_REFERER} !^http://domain.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

In the above you should replace http://domain.com with the URL your website uses and jpg|jpeg|gif|png|bmp with a pipe separated list of the file extensions you would like to protect. This will allow access to the listed file types only from the listed URLs.

For example if your website uses https://mydomainname.com as its URL and you want to protect .jpg and .jpeg images, you should add the following lines in the .htaccess file:

RewriteCond %{HTTP_REFERER} !^https://mydomainname.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://mydomainname.com$ [NC]
RewriteRule .*\.(jpg|jpeg)$ - [F,NC]

Was this article helpful?

Related Articles