Redirect https://domain.com to https://www.domain.com

We have multiple domains pointing to a static ip, we are mostly looking to generate https and redirect 301 to a new domain.

We want all traffic going to http://domain.com to go to https://www.domain.com this is easy
What we would like to do is https://domain.com redirect https://www.domain.com but first generate the certificate.

Can somebody point us in the right direction, keep in mind we have multiple domains

No worries, this is actually pretty simple, conceptually - you want both HTTP and HTTPS versions of your site to have the same behaviour (redirect to https://www). The Caddyfile plays out equally simply:

http://example.com, https://example.com {
  redir https://www.example.com
}

Caddy will handle the HTTPS site automatically, like any other Automatic HTTPS site.

You can simply list every site, HTTP or HTTPS version, in the site label list at the top - grouping all sites with the same redirection target together.

Hello Whitestrake,

Would there be a way to just have any domain, so I dont have to have a manual list. So dynamic?

:443, :80 {

  redir https://www.{host}{uri}

  tls email@domain.com
  tls {
    max_certs 10
  }
}

This is the initial solution this works great if I just wanted to move naked https domains to www . Now if i wanted to do domain redirect from one domain to do it on the caddy server. Is there a way to query an api to get the domain redirect it supposed to go.

If I might make some small recommendations:

http://, https:// {
  redir {
    if {host} not_starts_with www.
    to https://www.{host}{uri}
  }

  tls email@example.com
  tls {
    max_certs 10
  }
}

Using http://, https:// instead of :80, :443 makes it more explicit what you’re intercepting and allows for whatever ports you’re listening on to respond appropriately.

Adding the if check to the redir will prevents an inappropriate redirection in the case of a request for a domain you don’t actually serve.


You can always extend Caddy with a plugin to create this behaviour, but the current feature set has no such functionality.

The closest would be to simply proxy the request, either to another web server or directly via FastCGI, to a server running a scripting language. Have a PHP index file that checks the requested host and issues the redirect header appropriately, or an error otherwise.

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