Do you want to add HTTP security headers in WordPress?
HTTP security headers allow you to add an extra layer of security to your WordPress website. They can help block common malicious activity from affecting your website performance.
In this beginner’s guide, we’ll show you how to add HTTP security headers in WordPress.
What Are HTTP Security Headers?
HTTP security headers are a security measure that allows your website’s server to prevent some common security threats before it affects your website.
When a user visits your WordPress website, your web server sends an HTTP header response back to their browser. This response tells browsers about error codes, cache control, and other statuses.
The normal header response issues a status called HTTP 200. After which your website loads in the user’s browser. However, if your website is having difficulty then your web server may send a different HTTP header.
For example, it may send a 500 internal server error, or a not found 404 error code.
HTTP security headers are a subset of these headers and are used to prevent websites from common threats like click-jacking, cross-site scripting, brute force attacks, and more.
Let’s have a quick glance at some HTTP security headers and how they protect your website.
- HTTP Strict Transport Security (HSTS) tells web browsers that your website uses HTTPS and should not be loaded using an insecure protocol like HTTP.
- X-XSS Protection allows you to block cross-site scripting from loading.
- X-Frame-Options prevents cross-domain iframes or click-jacking.
- X-Content-Type-Options X-Content-Type-Options blocks content mime-type sniffing.
HTTP security headers work best when they are set at the web server level (i.e your WordPress hosting account). This allows them to be triggered early on during a typical HTTP request and provides maximum benefit.
They work even better if you are using a DNS-level website application firewall like Sucuri or Cloudflare. We’ll show you each method, and you can choose one that works best for you.
That being said, let’s take a look at how to easily add HTTP security headers in WordPress. Here are quick links to different methods so you can jump to the one that suits you.
1. Adding HTTP Security Headers in WordPress Using Sucuri
Sucuri is the best WordPress security plugin on the market. If you are using their website firewall service too, then you can set HTTP security headers without writing any code.
First, you will need to sign up for a Sucuri account. It is a paid service that comes with a sever level website firewall, security plugin, CDN, and malware removal guarantee.
During sign-up, you will answer simple questions, and Sucuri documentation will help you set up the website application firewall on your website.
After signing up, you need to install and activate the free Sucuri plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, go to Sucuri Security » Firewall (WAF) page and enter your Firewall API key. You can find this information under your account on Sucuri website.
Click on the Save button to store your changes.
Next, you need to switch to your Sucuri account dashboard. From here, click on the Settings menu on top and then switch to the Security tab.
From here you can choose three sets of rules. The default protection, HSTS, and HSTS Full. You will see which HTTP security headers will be applied for each set of rules.
Click on the ‘Save Changes in The Additional Headers’ button to apply your changes.
That’s all, Sucuri will now add your selected HTTP security headers in WordPress. Since it is a DNS-level WAF, your website traffic is protected from hackers even before they reach your website.
2. Adding HTTP Security Headers in WordPress Using Cloudflare
Cloudflare offers a basic free website firewall and CDN service. It lacks advanced security features in its free plan, so you will need to upgrade to its Pro plan which is more expensive.
To add Cloudflare to your site, see our tutorial on how to add Cloudflare free CDN in WordPress.
Once Cloudflare is active on your website, go to the SSL/TLS page under your Cloudflare account dashboard and then switch to the Edge Certificates tab.
Now, scroll down to the HTTP Strict Transport Security (HSTS) section.
Then, you can click on the ‘Enable HSTS’ button.
This will bring up a popup with instructions telling you that you must have HTTPS enabled on your WordPress blog before using this feature.
Click on the Next button to continue, and you will see the options to add HTTP security headers.
From here, you can enable HSTS, no-sniff header, apply HSTS to subdomains (if they are using HTTPS), and preload HSTS.
This method provides basic protection using HTTP security headers. However, it does not let you add X-Frame-Options and Cloudflare doesn’t have a user interface to do that.
You can still do that by creating a script using the Workers feature. However, creating an HTTPS security header script may cause unexpected issues for beginners which is why we wouldn’t recommend it.
3. Adding HTTP Security Headers in WordPress Using .htaccess
This method allows you to set the HTTP security headers in WordPress at the server level.
It requires you to edit the .htaccess file on your website. It is a server configuration file used by the most commonly used Apache webserver software.
Simply connect to your website using an FTP client or the file manager in your hosting control panel. In the root folder of your website, you need to locate the .htaccess file and edit it.
This will open the file in a plain text editor. At the bottom of the file, you can add the code to add HTTPS security headers to your WordPress website.
You can use the following sample code as a starting point, it sets the most commonly used HTTP security headers with optimal settings:
<ifModule mod_headers.c>
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options nosniff
Header set X-Frame-Options DENY
Header set Referrer-Policy: no-referrer-when-downgrade
</ifModule>
Don’t forget to save your changes and visit your website to make sure that everything is working as expected.
Note: Take care when editing code on your website. Incorrect headers or conflicts in the .htaccess file may trigger the 500 Internal Server Error.
4. Adding HTTP Security Headers in WordPress Using a Plugin
All in One SEO (AIOSEO) is the best SEO tool for WordPress and is trusted by over 3 million businesses. The premium version lets you easily add HTTP security headers to your website.
The first thing you’ll need to do is install and activate the AIOSEO plugin on your website. You can learn how to install and configure the plugin by following our step by step guide on how to set up All in One SEO for WordPress.
You should head over to the All in One SEO » Redirects page to add the HTTP security headers. First, you’ll need to click the ‘Activate Redirects’ button to enable the feature.
Once redirects are enabled, you need to click on the ‘Full Site Redirect’ tab and then scroll down to the Canonical Settings section.
Simply enable the ‘Canonical Settings’ toggle and then click the ‘Add Security Presets’ button.
You will see a preset list of HTTP security headers appear in the table.
These headers are optimized for security, you can review them and change them if needed.
Make sure you click the ‘Save Changes’ button at the top or bottom of the screen to store the security headers.
You can now visit your website to make sure that everything is working fine.
How to Check HTTP Security Headers for a Website
Now that, you have added HTTP Security headers to your website. You can test your configuration using the free Security Headers tool. Simply enter your website URL and click on the Scan button.
It will then check HTTP security headers for your website and will show you a report. The tool would generate a so-called grade label which you can ignore as most websites would get a B or C score at best without affecting user experience.
It will show you which HTTP security headers are sent by your website and which security headers are not included. If the security headers that you wanted to set are listed there, then you are done.
We hope this article helped you learn how to add HTTP security headers in WordPress. You may also want to see our complete WordPress security guide, and our expert pick of the best WordPress plugins for business websites.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Katrin says
Where in the .htacces do I have to put the code? above, in between # BEGIN WordPress or behind # END WordPress?
WPBeginner Support says
You would want to put code between the begin and end WordPress
Admin
Michelangelo says
Thank you so much for this article! It helped so much.
I wish you the best and that your sleeves never slip during dishwashing
WPBeginner Support says
Glad our article was helpful!
Admin
Karma Tsheten says
It worked for me but also it destroyed my design. when ever i add security header, design gets spoiled, any help
WPBeginner Support says
That sounds like your theme may be having a conflict for some reason. We would recommend reaching out to your theme’s support to see if they can see the root of the issue.
Admin
Nigel Mcilwaine says
Hi,
Unfortunately it didn’t work for me, even after clearing my cache. The site is on an Apache server, shared hosting is that going to affect the success?
cheers
WPBeginner Support says
You would likely want to check with your hosting provider to ensure they do not have caching or a rule on their end that would cause an issue with your security header
Admin
Neil Cheesman says
Hi
I have added the code to the .htaccess file but it hasn’t made any difference… the website redirects okay from HTTP to https but when testing I still get the message “Couldn’t find the HSTS header
in the response headers.”
WPBeginner Support says
Don’t forget to clear any caching on your site as that is the most common reason for that specific error after adding the code to your htaccess.
Admin
ed thomas says
did not work. still have:
Your website does not send all recommended security headers.
X-Content Type Options
X-Frame-Options
Permissions-Policy
WPBeginner Support says
You may want to check your caching as that is the most common reason it will not update. If you clear your site’s cache that should allow your headers to be found.
Admin
Mark Downing says
Thanks for the very helpful article. Our site went from an “F” to a “B” with no glitches after I pasted your code snippet into .htaccess.
WPBeginner Support says
Glad our recommendation was helpful
Admin