Kavach

Fix · Express / Node

Fix “HTTP redirects to HTTPS” on Express / Node

Answer every http:// request with a 301 redirect to the https:// URL (and enable HSTS once that works).

app.use((req, res, next) => {
  if (!req.secure) return res.redirect(301, 'https://' + req.headers.host + req.url);
  next();
});

Verify the fix

Re-run the HTTP redirects to HTTPS check after deploying. Test config changes in staging first and keep a rollback: a wrong header can break a site faster than a missing one.

Other stacks: nginx · Apache · Caddy · Cloudflare

Scan your site free → Monitor it continuously