The Strict-Transport-Security
(HSTS) header tells browsers to only communicate with your website over HTTPS, preventing insecure HTTP connections and mitigating certain attacks like man-in-the-middle (MITM).
.htaccess
file if you use one).Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
/etc/nginx/sites-available/your-site
).server
block:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
sudo systemctl restart nginx
header("Strict-Transport-Security: max-age=31536000; includeSubDomains");
Use a middleware like helmet
to set the header automatically:
const helmet = require('helmet');
app.use(helmet.hsts({ maxAge: 31536000, includeSubDomains: true }));
Or set it manually:
app.use((req, res, next) => {
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
next();
});
from flask import Flask, Response
app = Flask(__name__)
@app.after_request
def set_headers(response):
response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
return response
After setting the header, test your website to ensure it’s working:
Strict-Transport-Security
header with the correct value.Setting this header ensures that your website is only accessible via secure HTTPS connections, protecting user data and preventing potential attacks like man-in-the-middle (MITM) or downgrade attacks.
If you need further assistance, don't hesitate to reach out to your hosting provider or system administrator.