Apache HTTP server logo

Force HSTS in Apache .htaccess

Learn how to enable HSTS (HTTP Strict Transport Security) in Linux Apache .htaccess. I wrote about enabling HTTP Strict Transport Security (HSTS) in IIS earlier. But what about enabling HSTS in Apache .htaccess? Here is how.

Home » Force HSTS in Apache .htaccess

I wrote about enabling HTTP Strict Transport Security (HSTS) in IIS earlier. But what about enabling HSTS in Apache .htaccess? Here is how.

As explained in my post How to enable HTTP Strict-Transport-Security (HSTS) on IIS, HTTP Strict Transport Security (HSTS) is a web security policy mechanism which is necessary to protect secure HTTPS websites against downgrade attacks, and which greatly simplifies protection against cookie hijacking. Here is how to enable HSTS in Apache.

Add to your .htaccess file the following to force HSTS in Apache:

Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

This enables HTTP Strict Transport Security for your website in Apache, it uses max-age of 3153600 seconds (36,5 days), has includeSubDomains enabled for your SSL-/TLS-certificate on www. and uses preload.

Now the HTTP Strict-Transport-Security (HSTS) response header for your website is tackled in this article. If necessary, you can add an HTTP Content-Security-Policy (CSP) upgrade-insecure-requests header as well in your .htaccess.

The HTTP Content-Security-Policy (CSP) upgrade-insecure-requests directive instructs user agents to treat all of a site’s insecure URLs (those served over HTTP) as though they have been replaced with secure URLs (those served over HTTPS). This directive is intended for web sites with large numbers of insecure legacy URLs that need to be rewritten.CSP: upgrade-insecure-requests

Add in your .htaccess file:

Header set Content-Security-Policy: upgrade-insecure-requests;

That’s all.

One-time donation

Please take a second to support Sysadmins of the North and donate, your generosity helps!

Thank you very much! <3 ❤️

See also  Check IP address blacklist status in Bash
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
How to enable HTTP Strict-Transport-Security (HSTS) on IIS - Sysadmins of the North
2025-10-04 10:28 am

[…] While you’re at it, don’t forget to remove IIS Server header and ETag response headers. Need to force HSTS in Apache .htaccess? […]