Skip to content

How to prevent hotlinking

How to prevent hotlinking

Have you ever noticed your website slowing down for no apparent reason, or your hosting bills going up, even though your visitor numbers haven’t shot up? The culprit may be an invisible resource thief: hotlinking (also known as direct linking).

There is a great analogy that explains it:

Imagine your neighbour plugging an extension lead into a socket in your house and using your electricity to light their own home. You pay the bill; your neighbour gets the benefit. In the digital world, that is exactly what hotlinking is.

Other sites can embed images, PDF files or videos from your server straight into their own pages. Every time someone loads one of their pages, your server has to do the work and your bandwidth gets used up. In this guide I go through how to spot the problem and, above all, how to block it effectively on different platforms.

Five key facts

  1. Lost bandwidth is the main harm: hotlinking forces your server to spend its resources for the benefit of other sites. This can slow your own site down considerably and push up your hosting costs.
  2. The Referer header is the cornerstone of protection: most blocking methods are based on checking the HTTP Referer header. If a request doesn’t come from an allowed address, the door stays shut.
  3. How you do it depends on the server: on Apache servers the block goes in the .htaccess file, while on NGINX servers you use the valid_referers directive.
  4. CDN errors can be expensive: if you block requests at the CDN (for example with a 403 Forbidden error), make sure those errors are cached. Otherwise the blocked requests can still cause a huge spike in costs.
  5. Signed URLs give the strongest protection: if you are protecting valuable content, signed URLs are more secure than a Referer check, because they require a cryptographic signature.

1. What is hotlinking and why does it harm your site?

Technically, hotlinking happens when a third-party website adds a resource (such as an image) to its code using the direct URL of that resource on your site.

For example, if you have a stunning photo at yoursite.com/images/landscape.jpg, another site can write <img src="https://yoursite.com/images/landscape.jpg"> in its code. When someone visits that other site, their browser fetches the image from your server.

This looks harmless, but the effects can be serious:

  • Rising costs: if you pay for bandwidth or transferred data, you are paying for another site’s traffic.
  • Poorer performance: your server’s processing power goes on serving images to outsiders, which slows your site down for your real customers.
  • Skewed statistics: your server logs fill up with “junk traffic”, which makes your real visitor data harder to analyse.

The principle of the protection is simple: with every request, the server checks the HTTP Referer header. This header says which page the request came from. If the header doesn’t contain your domain (or isn’t on the allowed list), the server blocks the request.


2. Blocking hotlinking with .htaccess

Apache is one of the world’s most common web servers, and if you run WordPress on shared web hosting, for example, you are probably using Apache. In that case you have two main ways to stop resource theft.

Editing the .htaccess file

This method gives you full control over the rules. .htaccess is a configuration file that controls how the server behaves at folder level.

By adding the following block of code to your .htaccess file, you can block unauthorised requests:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yoursite.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

What does this code do?

  1. RewriteCond: these lines set the conditions. The code says: “If the referer is not empty AND the referer is not my site AND the referer is not Google…”
  2. RewriteRule: “…then block access (F = Forbidden) to every file ending in .jpg, .jpeg, .png or .gif.”

Hotlink protection on Tuonetti web hosting

If tinkering with code sounds daunting, my web hosting gives you a graphical option through Plesk.

  1. Log in to Plesk.
  2. Under WordPress -> Security, find the tool called Hotlink Protection.
  3. Click “Enable”.
The Hotlink protection toggle in Plesk, still switched off

NGINX is known for its speed, and its configuration differs a little from Apache’s. In NGINX, you use the valid_referers directive to define who may load your content.

Add the following block to your server’s configuration file (often nginx.conf or a site-specific file):

location ~ \.(gif|png|jpeg|jpg|svg)$ {
    valid_referers none blocked yoursite.com *.yoursite.com google.com bing.com;

    if ($invalid_referer) {
        return 403;
    }
}

Explanation:

  • location ~ ...: defines the file extensions the rule applies to (for example images and SVGs).
  • valid_referers: lists the allowed sources. none allows requests with no referer (important for browsers with privacy settings) and blocked allows requests where a firewall has hidden the referer.
  • if ($invalid_referer): if the request does not come from a listed source, the server returns a 403 Forbidden error.

4. CDN protection: blocking hotlinking with Cloudflare and AWS WAF

On today’s web, a large share of traffic goes through content delivery networks (CDNs). Blocking hotlinking at CDN level is often the most effective solution, because it stops the “thieves” at the edge of the network (edge locations), before the request can put any load on your own server.

Cloudflare Scrape Shield

Cloudflare has made this extremely easy.

  1. Go to the Cloudflare dashboard.
  2. Go to Security > Settings.
  3. Find Hotlink Protection.
  4. Turn the switch to On.

This stops other sites from linking to your images. It doesn’t affect search engine crawling, but your images then won’t show on sites such as Google Images, Pinterest or Facebook. To allow hotlinking for some images, exempt them with a Cloudflare configuration rule or put them in a folder named hotlink-ok.

AWS WAF and CloudFront

If you use Amazon Web Services (AWS), you can use the AWS WAF (Web Application Firewall) service.

  • You can create a WAF rule that checks the incoming Referer header.
  • This rule is attached to a CloudFront distribution.
  • This is the recommended approach when you want to protect static content globally and stop bandwidth drain from S3 buckets or EC2 instances.

5. Signed URLs: advanced protection

Sometimes a Referer check alone isn’t enough. The Referer header can be tampered with, or it can be missing in some applications. If your site sells digital products (such as e-books or course materials) or holds other sensitive material, you need heavier tools.

This is where signed URLs come in.

Signed URLs give you stronger protection than a Referer check. The link isn’t permanent; instead it contains:

  1. An expiry time: the link works for only 60 minutes, for example.
  2. A cryptographic signature: this confirms that the link was created on your server and hasn’t been tampered with.

6. Whitelist: allowing search engines, and common blocking mistakes

The most common mistake when blocking hotlinking is being too aggressive. If you block every outside request, you may accidentally “saw off the branch you’re sitting on”.

You absolutely must create a list of allowed sources (a whitelist). The most important ones to allow are:

  • Search engines: Google, Bing, DuckDuckGo. If you block these, your images disappear from Google Images, which can make your traffic collapse.
  • Social media: Facebook, Twitter (X), LinkedIn, Pinterest. These services fetch your images to create a preview card when someone shares your link. If you block them, your links look broken in social media feeds.

Tip: if you notice that a popular blog or a partner has linked to your content in a good way (bringing you traffic), add them to the allowed list separately as well.


7. Testing the protection, caching and keeping costs under control

Once you have the protection in place, the job isn’t over. A careless configuration can lead to surprise costs.

Testing with curl

Don’t trust the settings blindly; test them. You can use the curl command on the command line to simulate a theft:

curl -I -e http://evilsite.com https://yoursite.com/image.jpg

If the response is HTTP/1.1 403 Forbidden, the protection works.

Critical warning: CDNs and the cost risk

This is where many people make an expensive mistake. CDN services often charge for every request – error requests included.

If a popular site links to your image and you block it by returning a 403 Forbidden code, that site may still try to load the image thousands of times a minute. If the CDN doesn’t cache the error, you are billed for every attempt.

Solution: make sure Negative Caching (caching of errors) is switched on in your CDN settings. If the CDN “remembers” that the request is forbidden for the next hour, it turns away millions of requests at the edge at no extra cost.

Frequently asked questions

What exactly is hotlinking?

Hotlinking is when another site embeds resources (such as images, videos or PDF files) on its own page using a direct URL from your server. It uses up your resources without your permission.

Why should I prevent hotlinking?

Blocking it protects your bandwidth, keeps your site’s loading times steady, stops your server resources from being overloaded and saves you money on hosting costs.

How do I allow hotlinking for search engines or partners?

Add search engines and social media platforms to the list of allowed URLs (the whitelist) in your .htaccess or NGINX rules. This keeps your site just as visible in search results and social media shares as before.

Which file types can I protect from hotlinking?

Images (.jpg, .png, .gif, .bmp) are the most commonly protected, but you can extend the protection to PDF files, videos (.mp4), audio files (.mp3) and SVG vectors too.

What is the most effective way to prevent hotlinking if I use a CDN?

The most effective way is to use the built-in protection your CDN service offers (such as Cloudflare or AWS WAF). The Referer check then runs on the network’s edge servers, which stops unauthorised traffic as early as possible.

How can I replace a hotlinked image with a warning image?

You can use a rule that redirects unauthorised requests to a specific replacement image or HTML page (for example restricted.html). It is important that this warning image is not itself in a protected folder.

Are signed URLs a better solution than a Referer check?

Yes, for valuable or confidential content they are. They are a more robust solution, because they require the server to check a cryptographic signature and an expiry time before granting access.

Blog

Read next

All articles
Google Tag Manager: what it is and how to use it

Guides

Google Tag Manager: what it is and how to use it

Google Tag Manager transforms the way digital marketing work gets done. In this guide I explain what GTM is, why every website needs it and how to set it up step by step, from installation all the way to the Consent Mode v2 settings.

How to stop spam on your WordPress site

Guides

How to stop spam on your WordPress site

Spam is one of the most common headaches for anyone who runs a WordPress site, but today it is easier to fight than ever. This guide covers the most effective ways to stop spam on your WordPress site, from good-quality web hosting to smart plugins and privacy-friendly CAPTCHA alternatives.

What is DNS?

Guides

What is DNS?

Without DNS, we would have to remember long strings of numbers to visit websites. This article explains in plain language what DNS is, how a lookup happens in milliseconds and why optimising your name server settings can make your browsing faster and safer.

Need a hand with this? Ask me.

Tuomas LindroosI answer myself, usually within two hours. You can also send a message through the contact form.