I’m trying to redirect http //site.com, http //www.site.com and https //site.com to https //www.site.com with the following simple caddyfile: https://site.com https://site.com { redir to https://www.site.com bind 10.0.0.0 root /var/www/html/ }
If you remove the scheme (https://), Caddy will automatically handle HTTP → HTTPS redirection
In the redir directive docs, to and from are not literal; they should be swapped out for actual values - the 2nd example on the page you linked is relevant to what you’re attempting
Your redir directive may create a loop if this vhost is used for the entire config
With that in mind, I propose an alternate Caddyfile:
site.com, www.site.com {
redir 301 { # Permanent redirect specified for clarity
if {host} starts_with site.com # Only redir if there's no www
/ https://www.site.com{uri} # Preserve request URI
}
bind 10.0.0.0
root /var/www/html
}
Regarding point 1 and 2: these were just copy-paste errors I made when asking the question.
I did not know about point 3, thanks! And after rereading the docs on redir your point 4 is obvious now.
Anyway with your proposed configuration its working perfectly now!