I want to redirect example.com
to www.example.com
.
I found many examples here, on StackOverflow and Reddit, but most were for the inverse case (www -> naked). Those for this case (naked -> www) seem to be for an older version (pre-2020 / v1). I also studied the docs but found nothing.
In the traefik container's docker-compose.yml
, I defined a redirection middleware that I can use from multiple services:
traefik:
image: traefik
# ...
labels:
traefik.http.middlewares.nakedtowww.redirectregex.regex: ^https?://(?:www\\.)?(.+)
traefik.http.middlewares.nakedtowww.redirectregex.replacement: https://www.$${1}
traefik.http.middlewares.nakedtowww.redirectregex.permanent: true
Then for some service:
whoami:
image: traefik/whoami
# ...
labels:
traefik.enable: true
traefik.http.routers.whoami.rule: Host(`${DOMAIN}`, `www.${DOMAIN}`)
traefik.http.routers.whoami.middlewares: nakedtowww
When I visit either https://example.com
or https://www.example.com
it redirects to https://www.www.example.com
and responds with 404.
How do I fix it?