Caddy Redirect all HTTP Requests to HTTPS
Caddy is the only web server to use HTTPS automatically and by default. Caddy obtains and renews TLS certificates for your sites automatically.
You may learn more about the caddy configuration for HTTP to HTTPS redirection from this article.
Caddy does this by default with Automatic HTTPS. You can just leave your site scheme-agnostic, e.g.:
example.com {
...
}
Caddy will conduct a 301 redirect while keeping an HTTPS server listening and serving the actual site.
To manually redirect, you need to specify a definition for the HTTP version of your site. I'm sharing a few ways.
1. Blanket Redirect
Redirect the entire HTTP version of the site:
http://example.com {
redir https://{host}{uri} permanent
}
https://example.com {
....
}
2. Schema Redirect
Check for HTTP scheme, and redirect:
http://example.com, https://example.com {
redir {
if {scheme} is http https://{host}{uri}
}
....
}
3. Subdomain Redirect
Check subdomain, and redirect:
www.example.com {
redir https://example.com{uri} permanent
}
http://www.example.com {
redir https://example.com{uri} permanent
}
https://www.example.com {
redir https://example.com{uri} permanent
}
https://example.com {
....
}
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.