Stop Image Hotlinking in cPanel and Apache (Step by Step)
Other sites embedding your images use your bandwidth. Block hotlinking in cPanel or Apache .htaccess, then prove it works with one simple curl test.
The short answer
This guide shows how to stop other sites embedding your images by using cPanel Hotlink Protection, Apache SetEnvIf or mod_rewrite rules, Cloudflare, or the Nginx referer module. It explains how the Referer header works and why it can be faked. It includes curl tests with a forged Referer, troubleshooting for blocked own images and 500 errors, and a ready message for your host.
Reading options
On this page
To stop image hotlinking in cPanel, open cPanel > Security > Hotlink Protection, add your own domains to the allow list, list the image extensions you want to protect, and click Submit. On an Apache server without cPanel, a few lines in your .htaccess file do the same job.
Other sites embedding your images can quietly eat your bandwidth quota, and cPanel has a built-in tool that blocks it.
By the end of this guide you will be able to switch protection on, prove it works with a single curl command, and fix the problems it most often causes.
What hotlinking actually is
Hotlinking means another website shows your image on its page by pointing straight at the file on your server. The image appears on their site, but every view is downloaded from your hosting account, so your bandwidth pays for their traffic.
Think of it like a neighbour running a hose from your garden tap to water their lawn. The lawn looks great, the water bill lands on you, and you never agreed to any of it.
Made-up example: you run a recipe blog at www.example.com and post a lovely photo at /images/biryani.jpg. A content farm copies your recipe and keeps your image tag exactly as it was. Every reader of their page now pulls that photo from your server, and you see the bandwidth climb without any new visitors of your own.
If you are new to how hosting accounts, disk space and bandwidth fit together, the short primer on what web hosting is is a good place to start.
How servers spot it
Your server can tell who is hotlinking thanks to the Referer header. A header is a small piece of information a browser sends along with each request, and Referer holds the address of the page that asked for the file. MDN describes it as the absolute or partial address from which a resource has been requested, and notes that the name is a misspelling of "referrer".
When a page on another domain loads your image, the browser usually sends that other page's address in Referer. Hotlink protection reads it and refuses the request when the address is not on your allow list.
Two catches matter for everything below. The Referer header is optional, and it can be faked. Apache's own documentation says both things plainly, and the Nginx docs add that building a request with a fake Referer is "quite easy". So treat hotlink protection as a way to stop ordinary browsers from embedding your files. It is not a lock for private content.
Before you start
Gather these few things first. It takes five minutes and saves a lot of guessing later.
- cPanel access to the account that hosts the site, or SSH or file manager access if you are on plain Apache.
- A list of every domain and subdomain you use, including the
wwwand non-wwwversions, any image or CDN subdomain, and the address you use to log in to cPanel. - A copy of your existing
.htaccessfile, downloaded to your computer before you change anything. - A copy of any current hotlink allow list, if protection was set up before.
- About 15 minutes, including testing.
- Risk level: low to medium. A wrong allow list can hide your own images, and a typo in
.htaccesscan take the whole site down with a 500 error. Both are quick to undo if you kept your backups.
Turn on hotlink protection in cPanel
This is the main path for most people on shared hosting. cPanel's documentation describes the interface as a tool that stops other sites from embedding your files and using your bandwidth to serve them.
Step 1: Save your current settings
Open cPanel > Security > Hotlink Protection and look at the page before you click anything. If you see an existing list of allowed URLs, copy it into a text file on your computer.
This matters because cPanel's documentation warns that disabling hotlink protection deletes the entries in the URL allow list. If you ever toggle it off and on, you will want that list back.
Also download your current .htaccess from the site's main folder, using File Manager or an FTP client. It is a hidden file because its name starts with a dot, so turn on hidden files if you cannot see it.
Step 2: Click Enable
Click Enable. cPanel opens a view that shows two lists: Allowed referrers and Protected extension.
Click Go back to return to the settings form, where you can change both lists.
Step 3: Add the sites allowed to use your images
In the box labelled URLs to allow access, put one address per line. Include every version of your own domain, plus the URL you use to reach cPanel. cPanel's documentation specifically recommends allowing your subdomains and your cPanel login URL.
Here is a sample allow list for a site that uses both www and non-www addresses over HTTP and HTTPS.
http://example.com
http://www.example.com
https://example.com
https://www.example.com
https://cdn.example.com
https://cpanel.example.comIf you have already forced every visitor onto HTTPS, as in the guide to redirecting HTTP to HTTPS with .htaccess, you still lose nothing by keeping the http:// lines in the list.
Step 4: Choose the file types to protect
In the box labelled Block direct access for the following extensions, list the extensions you want to protect.
This is the set of common image formats most sites want to protect.
jpg,jpeg,png,gif,webpBe careful here. cPanel's documentation says file types in this box cannot be hotlinked regardless of other settings, so only list what you actually want blocked.
Step 5: Decide about direct requests
Tick Allow direct requests if you want people to be able to open an image by typing or pasting its address into the browser. A direct request usually arrives with no Referer at all, because there is no page that linked to it.
Leaving this ticked is the friendlier choice for most sites. It keeps bookmarks, shared links and some apps working.
Step 6: Optionally redirect hotlinkers
If you want blocked requests sent somewhere else, enter an address in Redirect the request to the following URL. Your home page is a simple choice.
Do not point it at an image with a protected extension on the same site. That image would be blocked too.
Step 7: Submit and look at the result
Click Submit. Then reopen cPanel > Security > Hotlink Protection and check that your allowed URLs and extensions are listed as you typed them.
Now open your own site in a private browser window and check that your images still load. If they do, move on to the testing section below.
Variants
The cPanel tool covers most shared hosting. Here is how the same idea works elsewhere, and what to watch for with each one.
cPanel, when the tool is missing
If you cannot find Hotlink Protection under the Security section, your host may have hidden it. Hosts control which tools appear in cPanel through WHM > Home > Packages > Feature Manager, so ask them to turn it on for your package.
Apache with .htaccess: the recommended method
Apache's documentation says you can block hotlinking without mod_rewrite, which is the module many older tutorials use. Its suggested approach sets a flag when the Referer matches your domain and then only serves images to requests carrying that flag.
Before editing, download a copy of your current .htaccess so you can put it back in seconds. Then add this block, which marks requests from your own domain and allows only those to fetch .jpg, .png and .gif files.
SetEnvIf Referer example\.com localreferer
<FilesMatch "\.(jpg|png|gif)$">
Require env localreferer
</FilesMatch>SetEnvIf creates an environment variable, which is just a named label attached to the request, based on something in the request. Here it adds localreferer whenever the Referer contains example.com. The Require env line then tells Apache to serve matching files only when that label is present.
Two details are worth knowing. SetEnvIf matching is case sensitive, and Apache offers SetEnvIfNoCase if you want case-insensitive matching. Also, this method has no exception for an empty Referer, so a person who pastes the image address straight into the browser is usually refused too.
Apache with .htaccess: the mod_rewrite alternative
Apache calls mod_rewrite a last resort, but says you may need it for more complex logic, such as serving a replacement image to hotlinkers instead of denying the request. Back up .htaccess again if you changed it since the last copy.
This block refuses image requests that carry a Referer from any other site, while letting requests with no Referer pass.
RewriteEngine On
RewriteCond "%{HTTP_REFERER}" "!^$"
RewriteCond "%{HTTP_REFERER}" "!www.example.com" [NC]
RewriteRule "\.(gif|jpg|png)$" "-" [F,NC]The RewriteEngine On line switches rule processing on, and it is needed once per .htaccess file. The first condition skips the rule when Referer is empty, which is what !^$ means. The second condition skips it when the Referer mentions www.example.com, ignoring case thanks to [NC]. The last line answers matching image requests with 403 Forbidden, which is what the [F] flag does.
If your site also answers on the bare domain, note that www.example.com will not match a Referer of https://example.com/. Add another condition line for each extra hostname you use.
Apache also shows a version that sends hotlinkers a replacement picture instead of an error.
RewriteCond "%{HTTP_REFERER}" "!^$"
RewriteCond "%{HTTP_REFERER}" "!www.example.com" [NC]
RewriteRule "\.(gif|jpg|png)$" "/images/go-away.png" [R,NC]Cloudflare
If your site sits behind Cloudflare, you can switch on its own Hotlink Protection. At the time of writing (24 November 2026), Cloudflare's docs say you turn it on from the Security > Settings page in the dashboard, where you can filter by Client-side abuse and switch Hotlink Protection to on.
Cloudflare denies image requests when the Referer does not include your domain name and is not blank, so blank-Referer requests are allowed. It covers gif, ico, jpg, jpeg and png files.
Its docs also warn that while this is on, your images will not display on sites such as Google Images, Pinterest and Facebook, although crawling is not affected. Any image inside a folder named hotlink-ok, placed anywhere on your site, is skipped by the check.
Nginx
Nginx does not read .htaccess files, so neither Apache method above works there. The Nginx wiki page on the topic opens with "You can't do this. You shouldn't." and goes on to explain the per-request cost of such files.
Nginx has its own referer module instead, configured in the server block rather than in a per-folder file. Its documentation gives this example, which accepts empty referers, referers stripped by a proxy, your own server names and a few listed patterns, and returns 403 for everything else.
valid_referers none blocked server_names
*.example.com example.* www.example.org/galleries/
~\.google\.;
if ($invalid_referer) {
return 403;
}On managed hosting you usually cannot edit this yourself, so it is a job for your host or server admin.
How to check it worked
Do not trust the dashboard alone. Test from the outside, using a Referer you control.
Test 1: Pretend to be another site
curl is a command-line tool for making web requests, and its -e option sets the referrer URL. The -I option asks only for the headers, so you see the status code without downloading the image.
Run this command, replacing the address with a real image on your site, to request it as if a spam site had embedded it.
curl -I -e "https://spam.example.net/" https://www.example.com/images/photo.jpgYou should see a first line containing 403. If you set a redirect URL in cPanel, or used the replacement-image rule, you will see a redirect status in the 300 range and a Location header instead.
Test 2: Pretend to be your own site
This command requests the same image with your own page as the referrer.
curl -I -e "https://www.example.com/" https://www.example.com/images/photo.jpgYou should see a 200 status. If you get 403 here, your own domain is missing from the allow list, or it is written differently from the address you tested.
Test 3: No referer at all
This command sends no Referer, the way a direct visit to the file usually does.
curl -I https://www.example.com/images/photo.jpgWhat you see depends on the method. With the mod_rewrite version, Cloudflare, or cPanel with Allow direct requests ticked, expect 200. With the SetEnvIf version, expect 403.
Test 4: A real browser
Open your own site in a private window and make sure every image shows. Then put an img tag pointing at one of your images on a page hosted on a different domain you control.
On that other page the image should appear broken. Open your browser's developer tools, go to the Network tab, reload, and you should see the image request answered with 403 or redirected.
Troubleshooting
Your own images vanished
Symptom: images on your own pages show as broken, and the Network tab shows 403 Forbidden for them.
Likely cause: a version of your domain is missing from the allow list. The usual suspects are the non-www address, the https:// version, or a separate image subdomain.
Fix: add every hostname you serve pages from, then repeat Test 2 for each one.
Images are blank on Google Images, Pinterest or Facebook
Symptom: shares and image search results show an empty box or no preview picture.
Likely cause: those sites display your image on their own pages, so the Referer is theirs and the request is refused. Cloudflare's docs confirm this happens with its Hotlink Protection, and the same logic applies to any Referer allow list.
Fix: decide whether that exposure matters to you. If it does, add those domains to your allow list, or on Cloudflare put the images you want shared in a hotlink-ok folder.
Your CDN, app or preview tool shows broken images
Symptom: images load on your site but fail inside a CDN, an email newsletter tool, a site builder preview or a mobile app.
Likely cause: the tool fetches or displays your images from its own hostname, which is not on your list.
Fix: find the exact hostname in your server's access log or the tool's documentation, add it to the allow list, and test again with curl -e using that hostname.
The whole site shows "500 Internal Server Error"
Symptom: right after editing .htaccess, every page returns 500 Internal Server Error.
Likely cause: a typo, or a directive your host does not allow in .htaccess. Apache's documentation says the error log will usually name the problem, with lines such as RewriteCond: bad flag delimiters or a message that a directive is "not allowed here".
Fix: upload your backup .htaccess to bring the site back first. Then read the error log, or ask your host for the matching log lines, correct the directive, and try again.
Nothing changed at all
Symptom: the forged-referer curl test still returns 200 OK.
Likely cause: your browser or a CDN is serving a cached copy, or the server is ignoring .htaccess. Apache's documentation explains that when AllowOverride is set to None, .htaccess files are completely ignored.
Fix: test with curl rather than a browser, purge any CDN cache, and if it still does not work, ask your host whether .htaccess overrides are allowed for your folder.
Pro tips and security notes
Protect only what costs you. Large photos and downloads are worth guarding, while tiny icons rarely are.
Keep the allow list in a text file in your own notes. Since disabling the cPanel tool wipes it, a saved copy turns a ten-minute rebuild into a paste.
Remember that Referer is controlled by the browser and the page. MDN warns the header carries privacy concerns and points to the Referrer-Policy header, which lets a page limit or drop the referrer it sends. A policy of no-referrer means no referrer information is sent, so some visitors will always reach you with a blank Referer.
Never use hotlink protection to hide private files. Anyone can fake a Referer with the same curl -e you used for testing, so real protection needs a login or signed links.
If you manage several domains for one business, as many teams comparing hosting services in the UAE do, keep one shared allow list and update it whenever you add a domain.
When to ask your host
Ask your host when the Hotlink Protection tool is missing from cPanel, when you are on Nginx, when .htaccess changes are ignored, or when you see a 500 error you cannot trace in the log. These usually need server-level access you do not have.
The one message worth forwarding
If you would rather hand this to your host or developer, copy and send this.
Hi, please enable hotlink protection for www.example.com.
Allow these referrers: http://example.com, http://www.example.com, https://example.com, https://www.example.com, https://cdn.example.com, and our cPanel login URL.
Protect these extensions: jpg, jpeg, png, gif, webp.
Please allow requests with no Referer so direct links still work.
Before changing anything, keep a copy of the current .htaccess and the current allow list.
After the change, please confirm that curl -I -e "https://spam.example.net/" https://www.example.com/images/photo.jpg returns 403, and that the same request with our own domain as the referer returns 200.
Thanks.Takeaway: allow every address you own, protect only the file types that cost you bandwidth, and prove it with one forged-referer curl test.
Reader questions
Does hotlink protection stop people from saving my images?
No. Anyone who visits your site can still view and save images, and the protection only stops other pages from embedding the files straight from your server.
Will hotlink protection hurt my SEO?
Cloudflare says its Hotlink Protection does not affect crawling, but images may stop displaying on sites such as Google Images while it is on. Add any sites you want showing your images to the allow list.
Can I protect PDF or video files too?
The cPanel extensions box and the Apache file pattern can list other file types, but this guide only verified the image types shown. Test any extra type with the curl check before relying on it.
Is hotlink protection enough to keep private files secret?
No. The Referer header is optional and easy to fake, so private files need a login or signed links instead.
Why do some normal visitors arrive with no Referer?
A page can limit or drop the referrer it sends with the Referrer-Policy header, and direct visits to a file have no linking page. That is why most setups let blank-Referer requests through.
Should I use the cPanel tool or edit .htaccess myself?
Use the cPanel tool if you have it, since it is quicker and harder to break. Edit .htaccess when you need custom rules or have no cPanel.
What happens to my allow list if I turn hotlink protection off?
In cPanel, turning protection off deletes the entries in the allowed URL list. Save a copy locally first if you ever plan to switch it off and on again.
Was this guide helpful?
Help us make the next update better.
Sources & further reading
- cPanel Docs: Hotlink Protection
- Apache HTTP Server: When not to use mod_rewrite
- Apache HTTP Server: mod_setenvif
- Apache HTTP Server: .htaccess files
- Cloudflare Docs: Hotlink Protection
- nginx: ngx_http_referer_module
- NGINX wiki: Like Apache .htaccess
- MDN: Referer header
- MDN: Referrer-Policy header
- curl man page
Originally published . About our editorial updates.



