If you're using Caddy and wondering why a specific route like /hello
isn't returning your expected response and instead always redirects, you're not alone. Consider this Caddyfile:
www.mysite.com {
respond /hello 200
respond "hello world"
redir https://anothersite.be
}
You might expect /hello
to return a 200 OK
or "hello world"
, but instead, every route redirects to https://anothersite.be
. Why?
Caddy doesn't process directives in the order they appear in your Caddyfile. Instead, it follows a fixed internal order, and redir
is executed before respond
. That means your respond
directives are never reached—they’re skipped after the redirect.
To explicitly control routing behavior, use handle
or handle_path
blocks. Here's the correct way to return "hello world"
only at /hello
, and redirect everything else:
www.mysite.com {
handle_path /hello {
respond "hello world"
}
handle {
redir https://anothersite.be
}
}
This ensures Caddy responds to /hello
as expected and redirects all other paths.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.