Redirect is running even when the error is triggered

In both cases, I enter with the same domain that starts with “www.”

Here the code is doing the redir:

:80 {
        #Block all domains that start with "www."
        # Check if the HOST include www. in the start
        @www_80_Hosts header_regexp Host ^www\.
        error @www_80_Hosts "Access Denied - You can not use www for A record. Fix your DNS records. Talk with the support." 403

        redir http://www.{host}{uri}
        #respond "Welcome to the testing page! {host}"
}

And here when I commend the redir and add the respond he return the user 403 error “Access Denied”:

:80 {
        #Block all domains that start with "www."
        # Check if the HOST include www. in the start
        @www_80_Hosts header_regexp Host ^www\.
        error @www_80_Hosts "Access Denied - You can not use www for A record. Fix your DNS records. Talk with the support." 403

        #redir http://www.{host}{uri}
        respond "Welcome to the testing page! {host}"
}

Why?

You are using a Caddyfile and the answer to “why?” is described here:

Many directives manipulate the HTTP handler chain. The order in which those directives are evaluated matters, so a default ordering is hard-coded into Caddy.

The directive redir is evaluated before error and that’s why you see a redirect happen.

The directive respond is after the error directive and that’s why you see the error in that provided example.

To quote the above referenced page one more time:

You can override/customize this ordering by using the order global option or the route directive.

Hope this helps.

2 Likes