Hotlink Protection: Why It Belongs to the Perimeter
Hotlink protection is one of those tasks that looks like a plugin feature and behaves like a server policy. The image request never reaches the application, so the application cannot refuse it. We place the rule at the CDN or in the web server configuration, test four cases, and watch the log for a week.
We meet this in audits more often than in new builds. A client’s gallery is embedded in forums, the monthly egress grows, and the site itself is fast and healthy. Nothing is broken. Somebody else’s pages are simply using the client’s disk as free image hosting.
Here is the decision process we use, with the configuration we actually ship and the part each rule breaks.
What hotlink protection does and does not do
It refuses a request when the Referer header points somewhere other than your own site. That is the whole mechanism. It is a bandwidth policy for careless embedding, and it is not access control: the header is supplied by the client, so a determined scraper sets it once and passes.
Keep those two jobs apart. Bandwidth policy belongs to the edge. Deciding whether a visitor is a scraper, a broken integration or a legitimate client belongs to the application, because only the application has the request context to answer it.
Where the bytes are actually spent
A request for a rendered page starts PHP-FPM, loads the framework, opens a database connection and runs application code. A request for /uploads/2026/11/photo.jpg does none of that: nginx or Apache finds the file and sends it.
That is why a plugin cannot protect static files on a normal setup. The request is answered before the application starts. On hosts where everything is proxied through PHP the rule would run, and the cost would be worse: you would boot the whole stack to send a 403.
The order matters in a second way. A check placed inside the application runs after the engine is up, which means the expensive work is already done. A check at the perimeter runs before any of it.
Rule 1: the CDN toggle
If the site sits behind Cloudflare, Scrape Shield has a Hotlink Protection switch. Turn it on and foreign referers are refused at the edge, with no origin traffic and no configuration file. For a site already proxied, this is the cheapest correct answer.
Two limits to write down. The rule covers proxied traffic only, so direct-to-origin requests and any hostname outside the proxy are untouched. And empty referers are allowed, which is why direct visits and privacy browsers keep working.
Rule 2: nginx valid_referers
On our own servers the rule is short:
location ~* \.(jpe?g|png|gif|webp|svg|mp4|pdf)$ {
valid_referers none blocked server_names *.example.com;
if ($invalid_referer) {
return 403;
}
}
none allows requests without a referer, blocked allows the ones a proxy has mangled, server_names allows the configured hostnames, and the wildcard adds subdomains. Everything else gets a 403 that costs a header instead of a megabyte.
Two notes from production. The location block must match the extensions you actually serve, and a site that serves images through a PHP handler needs a different rule, because the file never reaches this block. Check the access log before assuming which case you have.
Rule 3: Apache and LiteSpeed
On shared hosting and on Apache, the policy is one condition and one rule in .htaccess or in the virtual host:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule \.(jpe?g|png|gif|webp|svg|mp4|pdf)$ - [F,NC]
LiteSpeed reads the same directives, so the rule works on most shared plans without a support ticket. Keep it in the server configuration when you can: a rule inside .htaccess is invisible during a migration to nginx, and the next team discovers it when images stop loading after a move.
The four tests before you ship
- Own referer:
curl -I -e "https://example.com/blog/" https://example.com/uploads/photo.jpgreturns200. - Foreign referer:
curl -I -e "https://other.example/" https://example.com/uploads/photo.jpgreturns403. - Empty referer:
curl -I https://example.com/uploads/photo.jpgreturns200, or you have decided to block direct visits on purpose. - Direct file URL from a browser in private mode, because that is how a reader with a privacy extension arrives.
A rule that fails test three will generate complaints within a day. A rule that fails test one breaks the site’s own pages, which is worse and easier to miss in a quick check.
What breaks in production
Email clients fetch images without a referer. RSS readers and some mobile apps do the same. Social networks fetch preview images from their own infrastructure, so the preview either disappears or falls back to a placeholder. A staging copy on a subdomain is blocked by a rule written for production, and the developer who notices it is usually the client.
Google Images and Bing cache previews and re-fetch them; a strict rule reduces what they can show. If image search brings traffic, allow the search engines explicitly instead of turning the rule off.
Monitoring, not guessing
Two numbers make the rule safe to keep: the share of 403 responses for static files, and bytes served per day. A jump in the first means a legitimate client was caught. A flat second number after enabling the rule means the hotlinks were never the problem, and the bill has another cause.
We look at both after the change and once a month afterwards. That habit catches the case where a campaign, a newsletter or a new integration starts sending referers nobody expected.
When the rule is not enough
Perimeter rules cut volume. They do not identify a client that forges the header, rotates addresses or imitates a crawler. That layer is application work, and for WordPress sites we usually pair the perimeter rule with a security plugin that evaluates requests before the engine finishes loading.
If you are unsure whether your site is exposed, start with the access log and the top referers. If the numbers are unclear, our WordPress security team can read them with you, and WordPress optimization covers the rest of the delivery cost: caching, image formats and CDN settings.
Background: Cloudflare Hotlink Protection, the nginx referer module, Apache mod_rewrite, and the MDN reference for the Referer header.
