Enabling HTTP Strict Transport Security (HSTS) in a Few Easy Steps

What Is HTTP Strict Transport Security (HSTS)?

HTTP Strict Transport Security or HSTS is a web security policy that helps prevent man-in-the-middle (MITM) attacks that target HTTP connections. It instructs browsers to only access a website using HTTPS, never HTTP.

Why Is HSTS Important?

Enabling HSTS ensures that a browser will automatically redirect from HTTP to HTTPS for a website, securing all communications and preventing any insecure HTTP connections. This helps prevent MITM attacks where an attacker could spoof a website to capture data or install malware.

How Does HSTS Work?

When a browser first connects to a website over HTTPS that has an HSTS policy, it will get a response header telling it to only connect over HTTPS for a set period of time. The browser will then automatically convert any HTTP links or requests for that website to HTTPS.

Implementing an HSTS Policy

To enable HSTS on your website, you need to add a “Strict-Transport-Security” header to your HTTPS responses. This header will tell browsers to only use HTTPS for your domain for a set period of time, typically 6-18 months. You should start with a shorter max-age, like 3-6 months when first implementing HSTS.

Enabling HSTS is an important security step for any website. It helps ensure all your connections are encrypted and prevents dangerous MITM attacks. With a few quick code changes, you can drastically improve the security of your site and give your users peace of mind that their data and communications are protected.

Why Enable HSTS for Your Website?

Enabling HSTS policy for your website has some major benefits. For starters, it helps prevent man-in-the-middle attacks where hackers try to snoop on encrypted web traffic. By telling browsers to only connect to your site via HTTPS, it stops them from connecting through insecure HTTP connections.

Improved Security

With HSTS enabled, browsers will automatically redirect all HTTP requests for your domain to HTTPS. This eliminates the risk of accidentally connecting to your site over an unencrypted HTTP connection, which could expose sensitive data. HSTS also prevents attackers from intercepting HTTP traffic and redirecting users to malware or phishing sites.

SEO Ranking Boost

Google considers HSTS as a security best practice and enabling it for your site may give your SEO rankings a slight boost. While not a huge impact, every little bit helps if you want to rank higher in search results.

User Trust

By enabling HSTS, you are reassuring your users that you take security seriously. Users today are more aware of online threats like hacking, malware and identity theft. Taking measures like HSTS to safeguard their data and privacy will build additional trust in your brand.

Set It and Forget It

The great thing about HSTS is that once you enable it, you can pretty much set it and forget it. Browsers that have visited your site will automatically enforce the HSTS policy for your domain going forward. There’s no need to maintain or update it regularly. For most sites, enabling a long max-age value like 1-2 years works well.

So in summary, enabling HSTS for your website has significant benefits with very little downside or maintenance required. For any site that handles user data or transactions, HSTS should really be considered an essential security policy to implement.

Prerequisites for Enabling HSTS

Before you can enable HSTS on your site, you’ll need to make sure you have a few things in place.

A Secure Website

For HSTS to work properly, your site needs to use HTTPS, not just HTTP. This means you’ll need an SSL certificate installed on your web server to encrypt traffic between your site and visitors’ browsers. If your site isn’t already using HTTPS, you’ll want to set that up first before enabling HSTS.

Support for HTTPS

Your web server, like Apache or Nginx, will need to support HTTPS and have the ability to redirect HTTP traffic to HTTPS. Most mainstream web servers released in the past few years will meet this requirement, but double check to make sure yours does.

Compatibility with User Agents

HSTS works by having your site tell user agents—like web browsers—that it should only be accessed via HTTPS. For this to work, the user agents accessing your site must support HSTS. Most major browsers like Chrome, Firefox, Safari, and Edge support HSTS, but some older browsers may not. You’ll want to check browser support and consider your site’s audience before enabling HSTS.

Subdomain Inclusion (Optional)

If your site has subdomains (like blog.example.com or store.example.com), you’ll want to consider whether to include them in your HSTS policy. Including subdomains means that browsers will also require HTTPS for those subdomains and will redirect any HTTP requests to the HTTPS version. This helps ensure consistent security across your entire domain. However, it also means those subdomains must use HTTPS as well for HSTS to function properly.

Once you’ve checked off these prerequisites, you’re ready to start enforcing HSTS on your site. The next step is configuring your web server to send the Strict-Transport-Security header to user agents.

How to Enable HSTS in Your Web Server Configuration

To enable HSTS, you’ll need to configure your web server with a special header. This tells browsers that your site should only be accessed via HTTPS.

Apache Configuration

If you use the Apache web server, you’ll need to add a header to your site’s .htaccess file or virtual host configuration. Add this line:

Header set Strict-Transport-Security "max-age=31536000"

This will tell browsers to use HTTPS for your site for the next year (31536000 seconds). You can adjust the max-age to your liking.

Nginx Configuration

For Nginx, add this to your server block:

add_header Strict-Transport-Security "max-age=31536000";

Again, adjust the max-age as needed. This will add the HSTS header to all responses from that server block.


If you use WordPress, you can add the HSTS header using a plugin like WordPress HTTPS (SSL). Activate the plugin, enter your HSTS max-age, and it will automatically add the header for you.

Testing Your Configuration

To test that HSTS is working properly, first clear your browser’s cache and cookies. Then, try to access your site via HTTP. If all is working, your browser will automatically redirect to the HTTPS version of the site. The HSTS header tells your browser that HTTP should not be used to access the site.

Enabling HSTS hardens your site’s security by ensuring all communication is encrypted. It mitigates risks like cookie hijacking and man-in-the-middle attacks. For most sites, enabling HSTS is a simple step that provides substantial security benefits.

Setting the HSTS Header With Code

To enable HSTS, you need to configure your web server to send a Strict-Transport-Security header in its HTTP responses. The header specifies that browsers should only access the site using HTTPS for the duration specified.


If you’re using the Apache web server, you can set the HSTS header with the Header directive. In your site’s .htaccess file, add:


Header set Strict-Transport-Security “max-age=31536000”


This will instruct browsers to use HTTPS for 1 year (31536000 seconds).


For Nginx, you add the strict_transport_security directive to your server block:


add_header Strict-Transport-Security “max-age=31536000”;



On IIS, you can set response headers with the element. Add this to your web.config file:


  <add name="Strict-Transport-Security" value="max-age=31536000" />



If your site uses Cloudflare, you can enable HSTS through the Cloudflare dashboard. Go to your domain, select Crypto, and turn on Strict Transport Security. Enter the max-age value you want, like 31536000. Cloudflare will automatically handle setting the HSTS header for your domain.

Enabling HSTS is an important security step to ensure that visitors to your site are always accessing it over a secure HTTPS connection. By setting the header and max-age value, you can rest assured that browsers will require HTTPS for your domain into the future.

Testing That HSTS Is Working Correctly

Once you’ve enabled HSTS, it’s important to test that it’s working properly. After all, what good is enabling a security policy if it’s not actually doing what it’s supposed to?

Check Your Browser

The easiest way to check if HSTS is working is in your own browser. Clear your browser’s cache and browsing data to remove any stored HSTS policies. Then visit your site using HTTPS. Your browser should automatically redirect the request to HTTPS, rather than allowing you to access the site over HTTP.

Use an Online Tool

If you want to double check, there are free online tools that can scan your site for HSTS compliance. Two recommended options are the HSTS Preload List Submission and Qualys SSL Labs. Enter your domain and these tools will check if HSTS is properly enabled and give you details about your policy like the max-age value.

Verify the Response Header

You can also use a tool like cURL to directly check for the HSTS header in the response from your server. Run a command like:

curl -I -k https://yourdomain.com

You should see a response header that looks something like this:

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

This confirms your server is sending the HSTS header, with all the right directives, when someone accesses your site over HTTPS.

Check for Downgrades

Finally, you want to verify that your site is not accessible over HTTP anymore. Try accessing your domain using http:// instead of https://. You should receive an error message rather than your site content. This indicates requests are being properly redirected to HTTPS, and the HSTS policy is working to prevent insecure downgrades.

If you follow these steps and everything checks out, you can rest assured that HSTS has been properly enabled for your domain. Your site visitors will now automatically have their connections upgraded to HTTPS, and you’ve taken an important step in securing communications between your server and clients.

Troubleshooting Common HSTS Issues

So you’ve enabled HSTS on your site, but now you’re running into some problems. Don’t worry, these common hsts issues often have simple fixes.

Mixed Content Errors

If you’re seeing mixed content warnings, it means your site is loading insecure HTTP resources on an HTTPS page. This can happen if you have images, scripts, stylesheets or other assets that are still loading over HTTP. To fix this, you’ll need to update the URLs for those resources to use HTTPS instead. You may need to upload new versions of those files over HTTPS.

Redirect Loops

Redirect loops occur when two pages are redirecting to each other in an endless cycle. This can happen with HSTS if you have HTTP pages redirecting to HTTPS, and HTTPS pages redirecting back to HTTP. To resolve this, you’ll need to update your redirects so all HTTP pages redirect to HTTPS, and HTTPS pages do not redirect back to HTTP.

Certificate Errors

If your visitors are seeing certificate errors or warnings, it usually means there’s an issue with your SSL/TLS certificate. The most common causes are:

•Using an untrusted or self-signed certificate. You’ll need to purchase and install a certificate from a trusted CA like Let’s Encrypt.

•Certificate has expired. Renew your SSL certificate before it expires to avoid disruption.

•Certificate domain mismatch. Make sure the certificate you have installed matches the domain you’re using HSTS on.

•Protocol downgrade attacks. This can happen if there are insecure protocols enabled on your server that allow a downgrade from HTTPS to HTTP. Disable SSLv2, SSLv3, and TLS 1.0 on your server.

•Certificate chain issues. Make sure you have the full certificate chain installed, including any intermediate certificates. The chain should be installed in the correct order.

Following these troubleshooting steps should help get your HSTS policy working properly and securely. Be sure to also check for any updates to your web server, CMS or other software in case there were recent patches related to HSTS or HTTPS. And if issues continue, you may need to temporarily disable HSTS until the problems can be fully resolved.

Best Practices for Configuring HSTS

To enable HSTS on your website, there are a few best practices you should follow.

First, you’ll need to configure your web server to return an HTTP Strict-Transport-Security header. The header should include a max-age directive specifying how long, in seconds, the browser should remember to only allow HTTPS connections to your site. For maximum security, set max-age to at least one year (31536000 seconds).

You should also include the includeSubDomains directive. This tells browsers to apply the HSTS policy to all of your site’s subdomains. Without this, users could still access http versions of your subdomains, creating a security risk.

For added security, consider including the preload directive. This adds your domain to a list of HSTS preloaded domains that browsers automatically apply HSTS to. However, to be added to the preload list, your HSTS configuration must meet certain criteria. The max-age must be at least 18 weeks (10886400 seconds), you must redirect all HTTP requests to HTTPS, and you can’t have any mixed content issues.

To check that your HSTS header is configured properly, you can use security tools like securityheaders.io, observatory.mozilla.org, or htstsscan.com. These will analyze your header and point out any issues with your configuration.

Once you’ve configured the HSTS header, test to ensure it’s working properly. Try accessing your site over HTTP – you should receive a redirect to the HTTPS version. You should also test subdomains to confirm the includeSubDomains directive is functioning.

Enabling HSTS is an important security step for any website. By following these best practices, you can implement a strong HSTS policy that helps prevent man-in-the-middle attacks and ensures your users always access your site over a secure HTTPS connection.

HSTS FAQs – Your Top Questions Answered

You probably have a few questions about HTTP Strict Transport Security or HSTS, so let’s go over some of the most frequently asked ones.

What exactly is HSTS and why do I need it? HSTS is a web security policy that helps prevent man-in-the-middle attacks by telling browsers to only interact with a website over HTTPS. It makes your site more secure by eliminating risks from unencrypted HTTP connections.

How do I enable HSTS? Enabling HSTS is straightforward. You just need to add a response header to your web server config. The header is:

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

Replace SECONDS with the number of seconds you want the policy to last. Common values are 31536000 (1 year) or 63072000 (2 years).

Do I need to do anything else after adding the header? After deploying the HSTS header, you should also submit your domain to the HSTS preload list. This adds your domain to browser preloads so the policy is enforced even on the first visit. You’ll need to wait a few days after deploying HSTS before submitting to the preload list.

How do I test if HSTS is working? You can test if HSTS is functioning properly by:

-Visiting your site over HTTP and verifying you receive a 301 redirect to HTTPS

-Checking that your domain shows up on the HSTS preload list checker site.

-Using an incognito browser window to verify the HSTS policy is enforced even on the first visit.

-Using a tool like SSL Labs to scan your site and check for the HSTS header.

Does HSTS affect my SEO or analytics? Enabling HSTS should not negatively impact your SEO or analytics. Since it simply enforces HTTPS and redirects HTTP traffic, search engines and analytics tools will still crawl and access your site as normal over HTTPS. HSTS actually helps SEO by signaling to search engines that your site values security.

I hope this covers the most important details about HTTP Strict Transport Security and answers your pressing questions on the topic. Let me know if you have any other questions!