1. The problem I’m having:
Hello, I am running a multi-tenant application with Caddy handling traffic via on-demand TLS. I would like to permanently redirect all requests coming from www.* subdomains to their host domain.
So for example:
- www.example.com —301–> example.com
- www.any-domain.com —301–> any-domain.com
- www.* —301–> * (something like this)
Here is my current Caddyfile configuration (with comments added only here for clarity):
# Checks if caddy is allowed to issue a certificate for the requested domain
{
on_demand_tls {
ask http://app:3000/api/caddy/check-domain
}
email {$CADDY_EMAIL}
}
# Redirects all non secure traffic to HTTPS
:80 {
redir https://{host}{uri} permanent
}
# Issues a certificate for the requested domain based on on-demand TLS,
# Reverse proxies the request to a next.js app serving websites for multiple tenants
:443 {
tls {
on_demand
}
reverse_proxy app:3000
}
I cannot hard-code something like the example below because adding a new rule every time a new tenant adds his domain to my platform would be tedious. There surely has to be a more elegant solution similar to on-demand TLS magic from above.
# This will not work since I need a solution that works for all domains
www.example.com {
redir example.com permanent
}
# Is something like this possible to do?
www.* {
redir * permanent
}
If you have any ideas or hints that might get me on the right track please let me know I would appreciate it very much. Thank you ![]()
2. Caddy version:
caddy:2.10.2-alpine - via docker compose