Redirect all HTTP requests to HTTPS with a 301

I want to configure redirection from http to https

In Nginx it looks like this:
%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA

In the documentation I found only this:
%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA1

But as I understand it all depends on the parameter “X-Forwarded-Proto” and if it does not, then redirection will not be either.
The option in Nginx as I understand it does not depend on it, how to do the same in Caddy?

Caddy does this by default with Automatic HTTPS. You can just leave your site scheme-agnostic, e.g.:

example.com {
  ...
}

And Caddy will run a 301 Redirect listening on HTTP and serve the actual site on HTTPS.


To manually redirect, you’ll need to specify a definition for the HTTP version of your site, one way or another:

# Blanket redir for entire HTTP version of site
http://example.com {
  redir https://{host}{uri}
}

https://example.com {
  ...
}
# Combination, check for HTTP scheme and redirect
http://example.com, https://example.com {
  redir {
    if {scheme} is http
    / https://{host}{uri}
  }

  ...
}

In the latter, {scheme} is used to detect the protocol. https://caddyserver.com/docs/placeholders

4 Likes

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