Caddy Redirect all HTTP Requests to HTTPS

avatar
Published: Jan 22, 2023

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.

Table of Contents

Auto Redirection

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.

Manual Redirection

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 {
    ....
}

Comments

No comments yet…