This is tested with Traefik 1.7
This is how to redirect the root or base path to a sub path in Traefik using Docker labels:
Goals
https://example.com
->https://example.com/abc/xyz/
https://example.com/
->https://example.com/abc/xyz/
https://example.com/something
-> no redirect
We will match <begin of line>https://<any chars but not slash AS group1><slash or nothing><end of line>
and replace it with https://<group1>/abc/xyz/
.
In regex we have to escape a /
character by \/
. In docker-compose labels we need to escape again, so that it becomes \\\\/
.
We also need to escape $
to $$
because of docker-compose.
labels:
- "traefik.frontend.rule=Host:example.com"
- "traefik.frontend.redirect.regex=^https:\\\\/\\\\/([^\\\\/]+)\\\\/?$$"
- "traefik.frontend.redirect.replacement=https://$$1/abc/xyz/"
- "traefik.port=80"
- "traefik.enable=true"
Thank you for this gist. I want to extend it a bit, since I'm a beginner with traeffik and was struggling until I figured out that the respective middleware must be defined too.
So I want to add my full docker-compose.yml as example for other beginners to provide context to your regex logic defined in
redirectregex.regex
andredirectregex.replacement
(the relevant parts to this gist are the last few lines with comments):