- Add
Header always set Referrer-Policy "strict-origin-when-cross-origin"to theVirtualHost. WithHeader set, 404 pages and redirects are sent without it. - Browsers apply the last Referrer-Policy they recognise. Behind
mod_proxy_http, the application's header comes after Apache's and wins. PutHeader unset Referrer-Policybefore thealwaysline. - For a stricter policy on sensitive paths, set
no-referreragain in a<Location>. Apache replaces the virtual host's value there, error pages included. - Never mix
Header setandHeader always setfor this header: the response carries both, and the weaker one can end up last.
The short answer
Add the header to the VirtualHost of your site:
<VirtualHost *:443>
ServerName example.com
DocumentRoot "/var/www/example"
SSLEngine on
SSLCertificateFile /etc/ssl/example.com/fullchain.pem
SSLCertificateKeyFile /etc/ssl/example.com/privkey.pem
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</VirtualHost>sudo apachectl configtest && sudo apachectl gracefulalways keeps the header on error pages and redirects. Every snippet below
was run on Apache httpd 2.4.58, 2.4.62 and 2.4.69 and checked with curl, on
a page, a 404, a redirect and a proxied application.
strict-origin-when-cross-origin keeps full URLs on your own origin and sends
only the origin to other sites. The
Referrer-Policy overview compares the eight values
and explains when to choose a stricter one.
Before you start
The Header directive comes from mod_headers. Check that it is loaded, then
find your virtual hosts and every existing Referrer-Policy:
apachectl -M | grep headers
apachectl -S
grep -rniE "Referrer-Policy|ProxyPass" /etc/apache2/ /etc/httpd/ 2>/dev/nullIf headers_module is missing, enable it with sudo a2enmod headers on
Debian and Ubuntu, or uncomment its LoadModule line in httpd.conf.
Then check your HTML templates for a <meta name="referrer"> tag. It
overrides the header in the browser:
curl -s https://example.com/ | grep -i 'name="referrer"'You are looking for:
- An existing Referrer-Policy, in another file, in
.htaccessor in the application behind Apache. Two headers cause trouble (steps 3 and 5). Header setlines for this header. They behave differently fromHeader always set(step 2).- Paths whose URLs carry secrets, such as password reset or invitation links. They deserve a stricter policy (step 4).
Step 1: choose the value
| Your site | Apache line |
|---|---|
| Most sites | Header always set Referrer-Policy "strict-origin-when-cross-origin" |
| Never reveal where users come from | Header always set Referrer-Policy "no-referrer" |
| Keep full URLs on your origin, nothing elsewhere | Header always set Referrer-Policy "same-origin" |
Do not use unsafe-url or no-referrer-when-downgrade: both send full URLs
to other sites, and WebSec0 grades them as a warning. Do not wrap the value
in a second pair of quotes, as in "'no-referrer'": browsers receive the
quotes, treat the value as invalid and fall back to their default.
Step 2: keep it on error pages and redirects
Apache keeps two tables of response headers. Header set writes to the one
used for successful responses; Header always set writes to the one that is
also sent on errors and redirects. Our test with
Header set Referrer-Policy "strict-origin-when-cross-origin":
HTTP/1.1 200 OK
Referrer-Policy: strict-origin-when-cross-origin
HTTP/1.1 404 Not Found
(no Referrer-Policy header)
HTTP/1.1 301 Moved Permanently
(no Referrer-Policy header)With Header always set, all three responses carried the header. A 404 page
that links to your home page or loads analytics would otherwise fall back to
the browser default.
A Header line in the VirtualHost also reaches every <Location> and
<Directory>, even one that sets another header. Our test returned the
policy and Cache-Control for a file under a <Location "/assets/"> that
only set Cache-Control. To cover every site at once, put the line in the
server configuration; the
security headers hub for Apache shows one
file with all six security headers, loaded that way.
Step 3: one header behind a reverse proxy
Many frameworks send a Referrer-Policy of their own. With mod_proxy_http,
the application’s header lands in the default table and Header always set
adds yours in the other one. Our test, with an application sending
unsafe-url:
HTTP/1.1 200 OK
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: unsafe-urlBrowsers apply the last value they recognise, so users get the application’s
unsafe-url, and WebSec0 grades the same value as a warning. This is the
opposite of Nginx, where the proxy’s header comes last.
Pick one owner. If it is Apache, remove the application’s header from the
default table, then set yours in the always table:
<VirtualHost *:443>
# ...
ProxyPass "/" "http://127.0.0.1:8080/"
Header unset Referrer-Policy
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</VirtualHost>Our test then returned a single strict-origin-when-cross-origin, on the
application’s pages and on its 404s. Apache’s documentation notes that CGI
scripts and mod_proxy_fcgi, which runs PHP-FPM, put their headers in the
always table, where Header always set replaces them.
Step 4: a stricter policy on sensitive paths
Pages whose URLs carry a token, such as /reset/?token=…, should send no
referrer at all. Set the header again in a <Location>:
<VirtualHost *:443>
# ...
Header always set Referrer-Policy "strict-origin-when-cross-origin"
<Location "/reset/">
Header always set Referrer-Policy "no-referrer"
</Location>
</VirtualHost>What our test returned:
| URL | Referrer-Policy |
|---|---|
/ |
strict-origin-when-cross-origin |
/reset/ |
no-referrer |
/reset/does-not-exist (404) |
no-referrer |
Apache merges <Location> sections after the virtual host, so the later
set replaces the value. Unlike Nginx, the other headers of the virtual host
stay in place, with nothing to repeat.
Step 5: never mix Header set and Header always set
The two tables combine, and in our tests Apache always sent the always
table first, whatever the order of your lines. With Header set Referrer-Policy "strict-origin-when-cross-origin"
in the virtual host and the <Location "/reset/"> from step 4, our test
returned for /reset/:
HTTP/1.1 200 OK
Referrer-Policy: no-referrer
Referrer-Policy: strict-origin-when-cross-originThe always table came first, so browsers apply the weaker
strict-origin-when-cross-origin, and the reset page still sends its origin
to other sites. WebSec0 passes the page, so the scan does not reveal the
mistake. Use Header always set on every line for this header.
On shared hosting: .htaccess
Without access to the virtual host, the same line works in .htaccess,
provided the directory allows it with AllowOverride FileInfo:
# .htaccess
<IfModule mod_headers.c>
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>In our test, .htaccess headers were sent on pages, 404s and static files,
but not on a Redirect defined in the virtual host: Apache sends it before it
reads .htaccess.
Verify it
Check a page, an error page and a redirect. Use your own host name:
curl -sI https://example.com/ | grep -i "^referrer-policy"
curl -sI https://example.com/does-not-exist | grep -i "^referrer-policy"
curl -sI https://example.com/reset/ | grep -i "^referrer-policy"Each should print the policy you chose for that path. Count the headers to
catch duplicates. The result should be 1:
curl -sI https://example.com/ | grep -ci '^referrer-policy:'Then scan the site with WebSec0. The headers report shows the value it received and whether it passes.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Invalid command 'Header' |
mod_headers is not loaded |
a2enmod headers, or uncomment its LoadModule line. |
| Header missing on 404 pages or redirects | Header set without always |
Write Header always set. |
Two Referrer-Policy headers, the application’s last |
The proxied application sends one too | Add Header unset Referrer-Policy before the always line. |
| A stricter path policy has no effect | Header set in the virtual host comes last |
Use Header always set everywhere. |
| WebSec0 shows a warning | The last value is unsafe-url or no-referrer-when-downgrade |
Use strict-origin-when-cross-origin and keep one header. |
| WebSec0 fails a header that is present | No recognised value: a typo or extra quotes | Fix the value; browsers ignore it too. |
| Browser sends more than the header says | A <meta name="referrer"> in the page overrides it |
Remove the tag or make it match. |
| Changes do not show up | Reload skipped, or a CDN cached old headers | apachectl configtest && apachectl graceful, then purge the CDN. |
Server hub
Every header on Apache
Other web servers
Referrer-Policy on other servers
Caddy
Guide ↗Set Referrer-Policy on Caddy with the header directive, use no-referrer on sensitive paths with route, and stop a proxied app's weaker value from winning. Tested configs.
Nginx
Guide ↗Set Referrer-Policy on Nginx with add_header and always, keep it on every location, use a stricter policy on sensitive paths and avoid duplicate headers. Tested configs.
Traefik
Guide ↗Set Referrer-Policy on Traefik with the referrerPolicy option of the headers middleware, in labels or a file, and use no-referrer on sensitive paths. Tested configs.
Questions
Frequently asked questions
Where do I put Referrer-Policy in Apache?
In the VirtualHost of each site, or once in the server configuration to cover every site. Apache merges it into every <Location> and <Directory>, even one that sets other headers.
What is the Apache line for Referrer-Policy?
Header always set Referrer-Policy "strict-origin-when-cross-origin". It needs mod_headers. Check the configuration with apachectl configtest and reload with apachectl graceful.
Why is Referrer-Policy missing on my 404 pages or redirects?
Header set without always writes to the table Apache uses for successful responses only. Error pages and redirects that Apache generates use the always table. Write Header always set.
My response has two Referrer-Policy headers. Which one applies?
Browsers apply the last value they recognise, and WebSec0 grades the same one. Behind mod_proxy_http, our test returned Apache's header first and the application's last, so the application's value applied. Add Header unset Referrer-Policy before Header always set.
How do I use a different Referrer-Policy for one URL on Apache?
Add Header always set Referrer-Policy "no-referrer" in a <Location> for that path. It replaces the virtual host's value on pages and error pages, and the other headers stay in place.
Can I set Referrer-Policy in .htaccess?
Yes, with the same Header always set line, when the directory allows it with AllowOverride FileInfo. Wrap it in <IfModule mod_headers.c> if the module might be missing.
References
Sources
- Apache Module mod_headersApache HTTP Server Project
- Configuration Sections: how the sections are mergedApache HTTP Server Project
- .htaccess filesApache HTTP Server Project
- Referrer-Policy headerMDN Web Docs
