Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding HTTP Strict Transport Security to .htaccess

I would like to add HTTP Strict Transport Security directive to my .htaccess file. I've added the lock at the end of the code here but when I test Testing the HSTS preload process it show the setting not set. I checked my Apache config and see the headers module enabled.

What am I missing?

<Files .htaccess>
order allow,deny
deny from all
</Files>

<FilesMatch "\.(png|gif|js|css)$">
  ExpiresActive on
  ExpiresDefault "access plus 1 month"
</FilesMatch>

# disable directory autoindexing
Options -Indexes

ErrorDocument 400 http://%{HTTP_HOST}
ErrorDocument 401 http://%{HTTP_HOST}
ErrorDocument 402 http://%{HTTP_HOST}
ErrorDocument 403 http://%{HTTP_HOST}
ErrorDocument 405 http://%{HTTP_HOST}
ErrorDocument 404 /incl/pages/error404.php
ErrorDocument 500 http://%{HTTP_HOST}

RewriteEngine On

RewriteBase /
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# BEGIN GZIP
<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP

# Use HTTP Strict Transport Security to force client to use secure connections only
<ifmodule mod_headers.c>
    Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</ifmodule>

I tested here and here.

like image 334
santa Avatar asked Dec 03 '25 20:12

santa


1 Answers

For redirects you need to use always attribute:

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

From the mod_headers documentation:

You're adding a header to a locally generated non-success (non-2xx) response, such as a redirect, in which case only the table corresponding to always is used in the ultimate response.

like image 115
Barry Pollard Avatar answered Dec 05 '25 17:12

Barry Pollard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!