Better Way To Prevent Image Hotlinking with .htaccess

What is image hotlinking and how to prevent it?

Image hotlinking – It is someone directly taking the image URL from your server and embedding it on their website. It’s kind of image stealing, but most importantly losing your bandwidth. Usually, you have to pay for the server bandwidth. When someone hotlinking the image in their website, it will look like the image is serving from their server. In fact, is serving from your server. Every time the image load on another website, it cost us bandwidth.

So we need to prevent it, I would suggest using .htaccess in apache server.

Step 1. Create .htaccess

If you are not using any .htaccess file you have to create one. You can do this easily by opening a notepad and saving the file as .htaccess. Those who have .taccess just open it.

Step 2. Code that blocks hotlinking

So what does this code does in our website?

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
This is the list of the sites allowed for the link the images. so by default, all sites are blocked for hotlinking the images except those listed in the code.

RewriteRule \.(jpg|jpeg|png|gif)$ [NC,R,L]

In between the () are images you intended to block from hotlinking. To add more separate them with”|”

Change the to your image. So whenever someone hotlinking this image will show up. Make sure this image is not in your server as your server is hotlink protected.

Editorial Staff

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.