Automatic HTTP redirect is 301 - can we change it or add headers?

Hi all,

We are using Caddy’s automatic HTTPS support as follows:

website.com {
    tls my@email.com
    proxy / localhost:8080
}

Our production setup is more complex, but this is the essence of it. This works fine, but the automatic redirection sends a 301 redirect which we’d like a little more control over. Specifically:

The documentation is not clear on this point and I couldn’t find where in the source this magic happens. Does anyone have any suggestions?

Cheers,

- Bob -

I think I found the line of code that is responsible for the 301 redirect - seems there’s no way for it to be anything other than http.StatusMovedPermanently (301) - https://github.com/mholt/caddy/blob/master/caddyhttp/httpserver/https.go#L153

There aren’t any directives that modify the redirection behaviour of Automatic HTTPS. You should be able to split up your site into two vhosts for fine-grained control of the redirection process, though. It would look something like this:

http://website.com {
    redir / https://website.com{uri} 302 
    header / Cache-Control "max-age=3600"
}

https://website.com {
    tls my@email.com
    proxy / localhost:8080
}

This is the correct answer. :+1:

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