Every security headers a WordPress site must have

Last edited:
October 28, 2023
Reading time:
6 mins

/

Blog

/

Security

/

Every security headers a ...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

In today’s digital age, security is of paramount importance for any website, especially when it comes to WordPress sites. WordPress powers a significant portion of the internet, making it an attractive target for hackers and malicious actors. To bolster the security of your WordPress website, it’s crucial to implement HTTP security headers. These headers help protect your site, its users, and sensitive data from various threats. In this comprehensive guide, we will explore the essential HTTP security headers that every WordPress website should have.

Understanding HTTP Security Headers

HTTP security headers are part of the HTTP protocol sent by a web server to a client (usually a web browser). These headers provide instructions and information about how a web page should be processed or displayed. When correctly configured, security headers enhance the security posture of your WordPress site by mitigating certain types of attacks and vulnerabilities.

Implementing HTTP Security Headers in WordPress

Implementing these security headers in a WordPress site can be done in several ways:

  1. (We will present this way)Editing the .htaccess File: If you’re comfortable editing server configuration files, you can add the headers directly to your website’s .htaccess file.
  2. Using a Security Plugin: Many security plugins for WordPress offer settings to configure these headers. Popular security plugins like Wordfence, Sucuri, and Security Headers allow you to enable and customize security headers easily.
  3. Utilizing a Content Delivery Network (CDN): CDNs like Cloudflare often provide options to enable security headers as part of their services.

The instructions may vary slightly depending on your hosting provider’s control panel interface. However below is the most common way of editing the .htaccess file:

Log in to Your Hosting Control Panel:

Access your hosting account by logging in to your hosting provider’s control panel (e.g., cPanel, Plesk).

Locate the File Manager:

Look for the “File Manager” or a similar option in your hosting control panel. This tool allows you to manage your website files.

Navigate to the Root Directory:

In the File Manager, navigate to the root directory of your website. This is typically labeled as “public_html” or “www.”

Find and Edit the .htaccess File:

Locate the .htaccess file within the root directory. Right-click on it and choose “Edit” or “Code Edit.”

Make Your Changes:

In the code editor, make the necessary changes to the .htaccess file. It is preferred to add the below rules in the top of the file, before any other existing rules. After editing the .htaccess file, save your changes directly within the code editor. Download a backup of the default file for you to reupload it if something goes wrong.

Test Your Website:

Visit your website to ensure that everything is working as expected after the changes. Check for any issues or errors.

The Must-Have Security Headers for WordPress

1. HTTP Strict Transport Security (HSTS) Header

The HSTS header ensures that all communication between the user’s browser and your site occurs over a secure, encrypted HTTPS connection. This prevents attackers from downgrading the connection to HTTP, thus reducing the risk of man-in-the-middle attacks. Make sure you have an SSL certificate installed because you will not be able to access your website if you enable this header.

Implementation:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload;

2. X-Content-Type-Options Header

The X-Content-Type-Options header prevents browsers from interpreting files as something other than what they are. This can help mitigate certain types of attacks, such as MIME-sniffing attacks.

Implementation:

X-Content-Type-Options: nosniff

3. X-Frame-Options Header

The X-Frame-Options header mitigates the risk of clickjacking attacks by specifying which domains can embed your site in a frame or iframe.

Implementation:

X-Frame-Options: SAMEORIGIN

4. Content Security Policy (CSP) Header

CSP helps prevent cross-site scripting (XSS) attacks by specifying which sources of content are considered valid. It restricts the execution of scripts and resources to trusted sources only.

Implementation:

Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-source.com; style-src 'self' https://trusted-source.com; ...

5. Referrer-Policy Header

The Referrer-Policy header controls how much information is included in the Referer header when a user clicks on a link. Properly configuring this header can help protect user privacy.

Implementation:

Referrer-Policy: no-referrer-when-downgrade

6. Feature-Policy Header

Feature-Policy controls which browser features and APIs can be used by a web page. This header can help prevent unintended data leakage and attacks by restricting access to certain features.

Implementation:

Feature-Policy: geolocation 'none'; microphone 'none'; camera 'none'; ...

Wrapping up for Apache, Litespeed, Nginx and Openlitespeed

Below you can find the exact configurations for implementing the HTTP security headers mentioned earlier in .htaccess files or host settings for Apache, Nginx, and OpenLiteSpeed:

Apache and Litespeed Premium (using .htaccess):

# Enable HTTP Strict Transport Security (HSTS)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

# Prevent MIME-sniffing attacks
Header always set X-Content-Type-Options "nosniff"

# Protect against clickjacking attacks
Header always set X-Frame-Options "SAMEORIGIN"

# Implement Content Security Policy (CSP)
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' https://trusted-source.com; style-src 'self' https://trusted-source.com; ..."

# Configure Referrer-Policy
Header always set Referrer-Policy "no-referrer-when-downgrade"

# Define Feature-Policy
Header always set Feature-Policy "geolocation 'none'; microphone 'none'; camera 'none'; ..."

Nginx (in server block)

# Enable HTTP Strict Transport Security (HSTS)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
# Prevent MIME-sniffing attacks
add_header X-Content-Type-Options "nosniff";
# Protect against clickjacking attacks
add_header X-Frame-Options "SAMEORIGIN";
# Implement Content Security Policy (CSP)
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://trusted-source.com; style-src 'self' https://trusted-source.com; ...";
# Configure Referrer-Policy
add_header Referrer-Policy "no-referrer-when-downgrade";
# Define Feature-Policy
add_header Feature-Policy "geolocation 'none'; microphone 'none'; camera 'none'; ...";

Openlitespeed (in Webadmin Host Settings)

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-source.com; style-src 'self' https://trusted-source.com; ...
Referrer-Policy: no-referrer-when-downgrade
Feature-Policy: geolocation 'none'; microphone 'none'; camera 'none'; ...

Set up those HTTP Headers and secure your WordPress website!

Ensuring the security of your WordPress website is an ongoing process, and implementing the right HTTP security headers is a critical step. These headers help protect your site and its users from a range of security threats and vulnerabilities. By following the best practices outlined in this guide, you can significantly enhance the security of your WordPress site and provide a safer online experience for your visitors. Remember that security is a multi-layered approach, and combining HTTP security headers with other security measures is essential for comprehensive protection.