Http to https redirect not functioning properly

For those viewing this thread. Here is the solution.

In a private back and forth between Matt and myself he was able to point out my mistake.

In the older version of Caddy I had been using earlier this year v0.8.3 the following would work just fine:

# Redirect HTTP requests to HTTPS
http://domain.com {  
    redir https://domain.com{uri} 301
}

However, my guess is that some of the ambiguity of the above statement resulted in some tweaks in the newer version, which prevents the redir line from working as shown above in v0.9.3+.

The fix is simply to remove the 301 portion of that line above:

# Redirect HTTP requests to HTTPS
http://domain.com {  
    redir https://domain.com{uri} 
}

Problem solved!

From Matt:
The valid syntaxes for the redir directive:

redir from to [code]
redir to
redir [code] {
    from to [code]
}
redir [code] {
    if    a cond b
    if_op [and|or]
    ...
}

Notice that redir to code is not one of them. This is because it is ambiguous with redir from to, which is far more common. So you want either redir https://domain.com{uri} or redir / https://domain.com{uri} 301, which should do the same thing.

The 404 you’re getting is because the “from” path of your current configuration is “https://domain.com{uri}” and the “to” is “301”.

1 Like