Security HTTP Headers for WordPress: HSTS, CSP and the Rest Explained Without Jargon
← Back to the blog

Security HTTP Headers for WordPress: HSTS, CSP and the Rest Explained Without Jargon

·Ivan Pantić

In short: Security HTTP headers are instructions the server sends to the browser: HSTS forces HTTPS, X-Content-Type-Options stops files being misread, framing protection prevents clickjacking, Referrer-Policy protects privacy, and Content-Security-Policy limits where scripts may load from. They are set in .htaccess, the Nginx configuration or at the CDN, and CSP is introduced in report-only mode first.

When someone opens your site, the server does not send the browser only HTML, images and scripts. Every response also carries headers: short instructions the visitor never sees but the browser follows. Some of those headers are about security: they tell the browser to open the site only over HTTPS, not to display it inside other people’s pages, not to guess file types, and where it may load scripts from.

These headers do not replace updates, strong passwords or cleaning an infected site. But they close off several kinds of attack on the visitor’s side and, in the case of Content-Security-Policy, can limit the damage if a malicious script does end up on the site. In this article we explain each header in plain words, show how to check where you stand and how to introduce them without breaking the site.

How to check which headers your site sends

The quickest way is the curl -I command with the site address. It requests only the response headers and prints them one below the other. Without a command line, open the browser’s developer tools (the F12 key), go to the Network tab, reload the page and click the first request: the response headers are shown on the right.

Server response listing security headers, checked with curl
One command shows which headers the site actually sends.

There are also public tools that analyse headers and give a grade, such as securityheaders.com and Mozilla Observatory. They are useful for a quick look, but do not treat the grade as a goal in itself: it matters more to understand what each header does and whether it suits your site.

Five headers almost every site needs

Table of security headers and what each one protects against
Five headers that almost every WordPress site should have.

Strict-Transport-Security (HSTS)

Once a visitor has opened the site over HTTPS, this header tells the browser to open it only over HTTPS for the coming months, even if someone types the address with http:// or clicks an old link. This prevents an attack in which someone on public Wi-Fi intercepts the first, unencrypted request.

A typical value is max-age=31536000, meaning one year. Add includeSubDomains only if every subdomain works over HTTPS, including ones for email or old services. Use the preload option, which puts the domain on a list built into browsers, only when you are completely sure, because getting off that list is hard and slow.

X-Content-Type-Options

The value nosniff forbids the browser from guessing a file’s type. Without it, a browser could interpret a file the attacker uploaded as an “image” as a script. The header has no side effects and every site should have it.

Protection against framing

Clickjacking is an attack in which your site is shown invisibly inside someone else’s page and the visitor thinks they are clicking something else. Protection comes from the older X-Frame-Options: SAMEORIGIN header or the more modern frame-ancestors 'self' directive inside a Content-Security-Policy. SAMEORIGIN allows framing only on your own domain, which does not interfere with the WordPress theme customiser.

Referrer-Policy

When a visitor clicks a link to another site, the browser tells that site which address they came from. If your addresses contain sensitive data, such as order parameters, it can leak. The value strict-origin-when-cross-origin sends other sites only the domain, not the full address, and is a sensible compromise.

Permissions-Policy

This header switches off browser features the site does not use, such as the camera, microphone or location. If a malicious script ends up on the site, it cannot ask for access to those features. For most business sites it is sensible to switch off camera, microphone and location.

Content-Security-Policy: the most powerful and most demanding header

Content-Security-Policy (CSP) gives the browser a list of allowed sources for scripts, styles, images, fonts and connections to other servers. If the page tries to load a script from an unknown domain or send data to an address that is not on the list, the browser blocks it.

Why does that matter for security? A large share of attacks on online shops consists of injecting a script that reads data from the checkout form and sends it to the attacker. A well-configured CSP with a connect-src directive can stop that data leaving the page, even if the script was injected. If it does leave, that is a personal data breach with specific duties, described in hacked website and GDPR. The same goes for scripts that redirect visitors or load more malicious code from outside addresses.

Why is it demanding? WordPress and many plugins use inline scripts and styles in the HTML, and a site often loads content from many external services: analytics, ads, fonts, video, chat tools, payment providers. An overly strict CSP will block some of that and the site will partly break. That is why CSP is never introduced all at once.

Where headers are set

Apache and LiteSpeed: .htaccess

Most shared hosting runs Apache or LiteSpeed, so headers are added to the .htaccess file in the site root, inside a block that checks the headers module is available. Make a copy of the file before editing it, because a syntax error can take down the whole site.

Example of setting security headers in an .htaccess file
On Apache and LiteSpeed servers the headers go in .htaccess, inside a check that the module is available.

Nginx

On Nginx servers headers are set in the server configuration, which on managed hosting is usually done by support at your request (one of the questions worth asking before choosing a host). It is important to add the always parameter so headers are also sent with error responses.

Example of setting security headers in the Nginx configuration
On Nginx the same headers are set by the host or the server administrator.

CDN and firewall

If the site runs behind a CDN or firewall, headers can be added there too, using response modification rules. The advantage is that they do not depend on the server; the drawback is that they are easy to forget when settings change.

Plugins

There are also WordPress plugins that set headers. They are handy to start with, but then the headers are sent by PHP, so they do not cover static files such as images or PDFs. A server-level solution is more reliable.

How to introduce CSP safely in four steps

  1. List the external sources. Open the key pages (home, an article, contact, cart and checkout) with the Network tab open and write down every domain something is loaded from.
  2. Turn on report-only mode. The Content-Security-Policy-Report-Only header blocks nothing; it reports in the browser console what it would block. The site works normally while you collect data.
  3. Refine the rules. Watch the messages for a few days, add legitimate sources and remove ones you do not recognise. A message about a script from an unknown domain can also be a sign that the site is already infected.
  4. Switch to a real CSP. When the messages stop, replace the header with an actual Content-Security-Policy and keep monitoring the site after every major plugin change.
Browser console showing Content-Security-Policy violation reports in report-only mode
In report-only mode CSP blocks nothing; it only reports what it would block.

For the WordPress admin area it is often simpler to apply CSP only to the public side of the site, because the block editor and many plugins in the admin need looser rules. The biggest benefit is on the pages visitors see anyway, especially checkout pages.

Steps for safely introducing a Content-Security-Policy on a WordPress site
CSP is introduced gradually, not all at once.

For a company site or blog without a shop, a good starting set looks like this: HSTS for one year (without preload until you are sure), X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, Referrer-Policy: strict-origin-when-cross-origin, and a Permissions-Policy that switches off camera, microphone and location. These five headers can be introduced in an afternoon and rarely break anything.

Content-Security-Policy comes as a second step, in report-only mode. On simple sites the rules will be short: your own domain, an analytics service, perhaps fonts and video. After a week of monitoring you can usually switch to real blocking.

Online shops and checkout pages

For online shops headers carry more weight, because the site handles the data attackers want most. Attacks known as web skimming rely on the browser running any script it finds on the page without question and sending data to any address. CSP changes that: if the attacker’s domain is not on the list of allowed destinations for sending data, the browser will not send the request.

For shops we recommend applying strict rules first to the cart and checkout pages, where the set of external services is known and limited: your domain, the payment provider and perhaps a fraud prevention service. On the rest of the site the rules can be looser until enough reports have been collected. It is also important to review the reports regularly, because a new plugin or a change at the payment provider can add a new domain that needs to be allowed. Every new plugin is worth checking for known vulnerabilities first.

Alongside CSP, check that external services’ scripts load from their official addresses and not from copies on your server that nobody updates.

Headers you can forget about

X-XSS-Protection was recommended for years, but modern browsers removed the filter it controlled, and in some cases the header even caused problems. It is no longer recommended. The same goes for headers that reveal the server or PHP version, such as X-Powered-By: they help nothing and give the attacker information. Better to remove them.

Common mistakes

  • HSTS with includeSubDomains on a domain whose subdomains lack HTTPS. Visitors then cannot open those subdomains for months.
  • The same header set twice, for example both on the server and in a plugin, with different values. Browsers may then behave unpredictably.
  • CSP introduced straight away in blocking mode. The most common result is a broken checkout or contact form that nobody notices for days.
  • Headers only on the home page. Check articles, login pages and error responses too.

Do headers help if the site is already hacked?

Partly. If the attacker can change files on the server, they can change the headers too, so headers do not replace a cleanup. But CSP combined with monitoring of its reports can reveal very early that a script from an unknown address has appeared on the site, sometimes before Google or visitors notice. That is why we see it as part of monitoring, not just protection.

If the reports or the console show scripts you do not recognise, run the free website check. Among other things it looks at the external scripts the site loads. If a problem is confirmed, cleanup and closing the hole are described in the price list.

Conclusion

Security headers are one of the few protections you set once, that cost almost nothing and protect every visitor without them having to do anything. The five basic headers can be added right away. Content-Security-Policy takes more patience, but on shops and sites with forms it pays off more than almost any other single measure. The key is to go step by step: watch first, then block, and check again after every major change to the site that everything still works.

FAQ

Will security headers slow my site down?

No. They are a few lines of text with each response, which does not affect loading speed. HSTS can even speed up repeat visits slightly because it skips the redirect from HTTP to HTTPS.

Do headers affect Google rankings?

Not directly. HTTPS is a quality signal, and HSTS helps ensure the site always opens over HTTPS. Other headers do not affect rankings, but they protect visitors and the site’s reputation.

Can I use CSP if I rely on many external services?

Yes, but the rules will be longer and need maintenance. Start in report-only mode and apply strict protection first to the most sensitive pages, such as checkout.

Want to know whether your site is clean?

Run the free check: it takes less than a minute and needs no sign-up.

Run the check