Caddy HTTP to HTTPS Redirections

Caddy HTTP to HTTPS Redirections

caddy redirections

This article helps you understand the caddy configuration for HTTP to HTTPS redirection in details configuration.

Caddy Modern Defaults

“Caddy is the first and only web server to use HTTPS automatically and by default.”

Caddy provides the automatic redirection to https, and provisions TLS certificates for all the domains, associated with the websites in the Caddyfile configuration, keeping them renewed for free for a lifetime.

  • No downtime
  • Zero lines of configuration needed
  • No other tools like Open SSL certification renewal are needed in NGINX comparatively
  • It acts as a web server and serves the website on https by default

Caddy serves public DNS names over HTTPS using certificates from a public ACME CA such as Let’s Encrypt or ZeroSSL

Redirection Detail

Caddy is schema-agnostic with the following we can achieve HTTP to https by default with the following

Redirect all HTTP requests to HTTPS with a 301 redirect

Caddy will run a 301 Redirect listening on HTTP and serve the actual site on HTTPS, but not www.

example.com {
......
....
}

Manual Redirection

Manual definition of the HTTP and https will do the job in two ways

  1. Blanket Redirect
  2. Subdomain Redirect
  3. Schema Check Redirect

1. Blanket

`redir`, for the entire HTTP version of the site

http://example.com {
redir https://{host}{uri} permanent
}

example.com {
.......
....
}

2. Subdomain redirect

www.example.com {
redir https://example.com{uri} permanent
}

example.com {
redir https://example.com{uri} permanent
}

example.com {
redir https://example.com{uri} permanent
}

example.com {
.......
....
}

3. Schema Check Redirect

redir, check for HTTP scheme, and redirect

http://example.com, https://example.com {
redir {
if {scheme} is http https://{host}{uri}
}
.....
....
}

More details on caddy configuration and example configurations on my github repo is available.

Reference :

Docker Caddy Conf

Automatic Redirect in Caddy