A www and apex security headers mismatch happens when example.com and www.example.com, or a redirect target, answer with different protective headers. Fix it by serving the same Strict-Transport-Security, Content-Security-Policy and related headers from every reachable host variant, then confirm with curl -I against each hostname.
What is a www and apex security headers mismatch?
A host variant is a hostname that serves the same site and that a user can reach, such as the apex domain, the www label, or the target of a redirect. Visitors do not distinguish between them. They type either form, follow links to either, and expect the same protections.
A mismatch means the variants disagree. The apex might send HSTS, a Content-Security-Policy and X-Content-Type-Options, while www returns a bare response from a different server, CDN rule or default virtual host. The OWASP Secure Headers Project describes the headers a site should send, but they only help on the hostname that actually sends them.
What causes the mismatch?
Most mismatches come from infrastructure drift, not from a deliberate choice:
- Separate virtual hosts. The apex and www have their own server blocks, and only one received the hardening change.
- Different origins. The apex is served by the application, while www points at a CDN, a static bucket or a legacy host.
- Header inheritance rules. In nginx, an
add_headerinside alocationblock replaces the ones declared at server level instead of adding to them. - Redirect-only hosts. One variant exists only to redirect and is never given headers, because "it serves no content".
- Platform defaults. A hosting provider adds headers on its primary domain but not on the aliases.
Why is it rated medium?
Each header is only a partial control on its own, but a gap on one hostname lets an attacker choose the weaker door. An attacker, or just a link in a phishing email, can point users at the unprotected variant.
Concrete consequences:
- HSTS gap. A first visit to the variant without HSTS stays open to SSL stripping on a hostile network. Whether HSTS on the apex covers www depends on
includeSubDomainsand on the browser having already seen the header, so you cannot rely on it. - Framing and script injection. Without a CSP or
frame-ancestorson one host, clickjacking and injected scripts that the other host blocks work again. - Cookie scope. Cookies set for the parent domain are sent to both hosts, so weakness on one exposes sessions created on the other. See cookie name prefixes for hardening that.
Auditors flag this because a control is only evidenced if it holds for every in-scope hostname. The cost is a failed assessment line, and potentially a real downgrade or session-theft incident through the weakest variant.
How does the scanner detect and rate it?
SetEnforce resolves the apex and www forms of the scanned domain and follows redirects to find the final target. It then requests each reachable variant over HTTPS and records the security headers returned. The finding is raised when the variants differ: a header present on one and missing on another, or present with a materially weaker value.
The evidence lists each hostname with the headers it returned and the ones it lacked, so you can see which variant is behind. The check is rated medium. A variant that does not resolve or does not answer is not compared, because there is nothing for a user to reach. Details of header comparison, such as which values count as weaker, can vary between scanner releases, so read the evidence on your own finding.
How do I fix it?
Define the headers once and apply them to every host that answers. The goal is a single source of truth, not two copies that drift.
nginx
Use one server block that lists both names, or an include file shared by both:
# /etc/nginx/snippets/security-headers.conf
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
server {
listen 443 ssl;
server_name example.com www.example.com;
include snippets/security-headers.conf;
# If any location block uses add_header, include the snippet there too.
}
The always flag matters: it adds headers to error and redirect responses too.
Apache
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</VirtualHost>
CDN or separate origin
If www is served from a CDN or bucket, configure a response headers policy there with the same values. Also check that both names have valid certificates. If the HTTPS response is broken on one variant, the redirect chain never reaches the protected one. A CSP written for the application may need adjusting before it fits static or legacy content, so test it in report-only mode first. Make sure HTTPS pages do not load mixed active content under the new policy.
Browsers honor HSTS only when it arrives over HTTPS. Putting it on a plain HTTP redirect has no effect, so make sure the HTTPS response of each variant sends it. See MDN's Strict-Transport-Security reference for directive details.
How do I confirm the fix on re-scan?
First compare the variants yourself:
for h in example.com www.example.com; do
echo "== $h"
curl -sI "https://$h/" | grep -iE '^(strict-transport|content-security|x-content-type|referrer-policy|location)'
done
Follow any redirect and check the final target too:
curl -sIL https://example.com/ | grep -iE '^(HTTP|location|strict-transport|content-security)'
Every hostname should print the same security headers with the same values. Then trigger a new scan. The finding should clear when no variant shows a missing or weaker header. If it persists, the evidence names the host still out of line. Caches and CDN propagation can delay this, so purge the cache and retry before concluding the change failed.
Common questions
Do I need headers on a host that only redirects?
Yes, on its HTTPS response. A redirect-only host is still reachable, and a user's first request hits it. Sending HSTS and the other headers there closes the gap. Browsers ignore HSTS on plain HTTP responses.
Does HSTS on the apex protect www?
Not reliably. HSTS covers subdomains only when includeSubDomains is set and the browser has already received the header from the apex. A first visit to www gets no protection unless www sends the header itself, so configure both explicitly.
Should the Content-Security-Policy be identical on both hosts?
Usually yes, when both serve the same site. If www is genuinely a different application, it can have a different policy, but it must still be at least as strict in the areas the scanner compares. Document any intentional difference so reviewers understand it.
Key takeaways
- Treat the apex, www and every redirect target as one site and give them identical headers.
- Define headers in one shared snippet or policy to prevent drift, and mind nginx
add_headerinheritance. - Send HSTS over HTTPS on every variant, and do not rely on
includeSubDomainsfrom another host. - Verify with
curl -Iper hostname, then re-scan. SetEnforce compares the headers each reachable variant returns and reports the differences as evidence.