# HSTS

> The HSTS (HTTP Strict Transport Security) is a mechanism that forces browsers to connect to a site only over HTTPS.

## What is HSTS and how do I enable it?

The mechanism configured with the `Strict-Transport-Security` header that tells the browser to always use HTTPS for the domain. Protects against protocol downgrade attacks and cookie hijacking.

- Topic: Security
- Type: technique

## Example

Wrong:

```http
Strict-Transport-Security: max-age=3600
```

Right:

```http
Strict-Transport-Security: max-age=31536000; includeSubDomains
```

A one-year `max-age` keeps browsers on HTTPS between visits, while one hour leaves frequent gaps for downgrade attacks.

## Common mistakes

- Enabling `includeSubDomains` and breaking an internal subdomain that has no HTTPS.

## Key points

- The `Strict-Transport-Security` header is sent only over HTTPS responses.
- `max-age` is long in production, and a short value is used only while testing.
- `includeSubDomains` and 'preload' are set only when every subdomain works over HTTPS.

## Interview questions

### How do I get my site on the HSTS preload list?

Serve the header with a `max-age` of at least one year, `includeSubDomains` and 'preload', redirect HTTP to HTTPS, then submit the domain to the preload list.

### Can I undo HSTS?

Send `max-age=0` over HTTPS to clear it in browsers that visit again. Removal from the preload list is slow, so preload only when you are sure.

### Does HSTS replace the HTTP to HTTPS redirect?

No. The redirect is still needed for the first visit, and HSTS then makes the browser skip plain HTTP on later visits.

## Related cards

- [HTTPS](https://wonderium.dev/web/https.md)
- [TLS](https://wonderium.dev/web/tls.md)
- [MITM](https://wonderium.dev/web/mitm.md)
- [CSP](https://wonderium.dev/web/csp.md)
- [HTTP Header](https://wonderium.dev/web/http-header.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/hsts
- Last updated: 2026-11-25
