How to Prevent Hotlinking – Easy Way

0 Flares 0 Flares ×

In my last post I described about what is hotlinking and its effect on your website. In this post I am going to explain different ways to prevent hotlinking. Hotlinking can badly damage to your website. It’s a bad practice of bandwidth stealing. So prevention of hotlinking is a first task for any webmaster.

Here I am going to explain two ways of preventing hotlinking: a) Using .htaccess file b) Directly from your CPanel.

 

Prevent Hotlinking Using .htaccess file

 

Using an .htaccess file to prevent hotlinking is an easier way. You just need to add some lines of code to your .htaccess file, if you do not have an .htaccess file then you can create a new one on for your website and place it under the web root.  Just add the below lines of code to your .htaccess file.

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

 

Make sure to replace yourdomain.com in the above code with your own domain name. This code prevents your image, JavaScript and CSS files from hotlinking. This rule allows only your domain to link to any file. Whenever someone tries to hotlink any of your image files they will just get a broken link on their web page. You can modify the last line code to add any type of file that you would like to prevent from hotlinking.

 

Configure It directly from your CPanel

 

This method is useful if you do not want to modify your .htaccess file. If you are using CPanel you can do the configuration in the security section.

Login to your CPanel and click on the Hotlink Protection link on the security section.

prevent hotlinking

 

 

 

 

 

 

 

In the next screen you can enter all the URLs which you want to allow for hotlinking, enter the file types to prevent from hotlinking and then click on enable button. Make sure to select the checkbox against Allow direct request, this will allow to view the image file directly in the browser. See the images below for a quick reference.  

prevent hotlinking

Guru

+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×