How To Prevent Image Hotlinking in WordPress

Updated November 15th, 2017
Updated November 15th, 2017
Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on pocket
Share on email
Share on print

Have you recently noticed that your images are being used in other websites, directly from your server? Have you found out that someone is stealing your images and using them in their sites by directly linking them to your server, without your permission? If any of these are true, then you are a victim of image Hotlinking.

In this article I will discuss how to prevent image Hotlinking in a simple way. Are you ready to protect your images? Yes? Let’s get started.

Why Prevent Image Hotlinking?

Before starting the actual discussion, let’s make it clear why you should not allow Hotlinking to your images. First of all, and in most cases, you have bought the images from somewhere, which means they belong to you. Anyone else wanting to use them should get your permission before doing so.

Second and more importantly, image Hotlinking could eat up your bandwidth allocation. As the original images are stored on your server, every time someone loads the sites that hotlinked the images they load from your server. Therefore, the bandwidth is actually consumed from your allotted quota. This is a two-edged sword for you. And you need to neutralize the threat as soon as possible.

Okay, that was enough prologues. Now, let’s start the actual process.

Preventing Image Hotlinking

While there are a few different ways of preventing the unwanted use of your images, we will discuss the smartest and most efficient way here. Why is this way smarter than others?

  1. Because it will allow you to specify which sites to block and which sites to allow
  2. It will allow you to accept or deny blank referrers
  3. If image Hotlinking is detected, you can deliver a custom image instead of the original image to the culprit websites.

Now, our chosen way uses the .htaccess file to prevent image Hotlinking. If you are not using the .htaccess file yet, this is the perfect time to start using it. To create the file, open a new file in your favorite text editor (not MS Word, you need specialized text editors like Notepad++, Sublime Text, etc.). Save the file as .htaccess, no extension please.

And if you already have the .htaccess file in your server, download it. We need to add a few lines of code to the file.

The Final Step

Open the .htaccess file and paste in the following code block:

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain2.com [NC]

RewriteRule \.(jpg|jpeg|png|gif)$ https://i.imgur.com/g7ptdBB.png [NC,R,L]

I don’t expect you to understand any of these codes. It is my job to explain them to you. So, let’s begin with the first line. This line turns on the rewrite engine.

The second line allows blank referrers. That is, some users browse the internet with their firewall turned on. Those users won’t provide any referrers information. This line of code will deliver the images to them properly.

The third and fourth lines are the allowed sites to use the images. No other websites will be able to hotlink your images anymore. Don’t forget to replace yourdomain.com, yourdomain2.com with your own domain(s). You can add additional domains too.

Finally, the fifth line replaces any unauthorized use of your images with the image defined in that line. You can use a custom image for this purpose. Just upload the image to your server and update the file path in the fifth line.

That’s all. Now save the file and upload it to your web server. If you have followed me accordingly, you have successfully saved your images from Hotlinking.

So that was not very hard, right? After all, you should follow these types of security measures to protect the uniqueness of your website. And saving some bandwidth will come in handy too.

Did you find the article useful? If yes, please share it with others so that they can save their images from Hotlinking too. And if not, let me know what else you want to know. I will try my best to provide some answers.

Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on pocket
Share on email
Share on print
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email

Responses

Your email address will not be published. Required fields are marked *

WPLearningLab