Adding the HSTS (HTTP Strict Transport Security) header to your WordPress site ensures that browsers always connect via HTTPS. Here are the most common methods to implement it.
Prerequisites
Important: Before enabling HSTS, make sure your site is fully working on HTTPS. Once HSTS is enabled, browsers will refuse to connect via HTTP for the specified duration. If your SSL certificate has issues, visitors won't be able to access your site.
Method 1: Apache (.htaccess)
Add the following to your .htaccess file in the WordPress root directory:
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</IfModule>
Method 2: Nginx
Add this to your Nginx server block configuration:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
Method 3: WordPress functions.php
Add the header via PHP in your theme's functions.php:
function add_hsts_header() {
header('Strict-Transport-Security: max-age=31536000; includeSubDomains; preload');
}
add_action('send_headers', 'add_hsts_header');
Recommended Approach
- Start with a short
max-age(e.g., 300 seconds = 5 minutes) to test. - Verify your site works correctly over HTTPS.
- Increase to
max-age=31536000(1 year). - Add
includeSubDomainsif all subdomains support HTTPS. - Optionally add
preloadand submit to the HSTS preload list.
Verify Your HSTS Header
After adding the header, run a new InspectWP scan on your site. The security headers section should now show the Strict-Transport-Security header as present.