Automatic SSL without redirecting HTTP traffic to HTTPS

I am trying to configure Caddy to do the following:

I can get the second half to work with the automatic SSL via Lets Encrypt. The problem, however, is that Caddy is forcing the HTTPS redirection which is unwanted. Here’s my Caddyfile so far:

:443

gzip
log /var/log/caddy.log

tls {
        max_certs 10
}

redir 301 {
        {scheme}://www.{host}{uri}
}

We’ll be switching the max_certs directive for getting an approved list of hosts from an API endpoint we have, but during testing the max_certs suffices.

How can I suppress the HTTP → HTTPS redirection?

Simple enough - instead of letting Automatic HTTPS configure the HTTP listener, you just add HTTP to your list of site labels, and Caddy will serve HTTP exactly like it serves HTTPS. Then you add the redirect to www on top. Now, no scheme redirection takes place.

http://, https:// {
  gzip
  log /var/log/caddy.log
  tls {
    max_certs 10
  }
  redir {
    if {host} not_starts_with www.
    / {scheme}://www.{host}{uri}
  }
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.