Http/https redir for any subdomain

In caddy 1 I was using

# Main http/https redirect for any subdomain requests on port on port 80/http to equivalent https
*.kebler.net:80 {
   redir https://{label1}.kebler.net{uri}
}

but the part not working in caddy two is {label1}

for example test.kebler.net will redirect as https://.kebler.net so obviously it’s not picking up the subdomain prefix.

I can’t find any caddy2 docs on how the url is parsed so maybe it’s a new variable name for anything proceeding the domain??? In any case can’t get this basic redirect to work in caddy2 like caddy1

Looking at https://caddyserver.com/docs/caddyfile/concepts#placeholders the placeholder should be written as {labels.2}.

I did not test this, but https://caddyserver.com/docs/modules/http says that labels are 0-based from right (therefore index 2 should be your subdomain).

Btw: Caddy will automatically redirect http traffic to https (if you do not specify port or schema for your host in the Caddyfile)

2 Likes

Just a note, the {labels.*} syntax is only available in v2.1+ so upgrade to the latest if you haven’t already, or use the long-form otherwise.

I confirm that labels.2 is correct for the subdomain (using 2.1.1). I’ve had that http/https redirect stanza since early caddy1 and didn’t realize that this redirect is no longer necessary so the best is just to leave it out which per @duff does indeed redirect all requests automatically!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.