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"
@idhamari sorry for late answer, but for me this is working:
Changes that I made:
Host(myexample.com)
, because you only have one hostadd-context
toadd-context1
andadd-context2
-> you need unique names and not reuse it between servicesPathPrefix
instead ofPath
, so we can match/app1
and/app1/
^(.+/app1)$$
will match anything with ends with /app1traefik/whoami:v1.8.7
instead of nginx only for easier testingTry also to look at servername:8080 to see the traefik dashboard. There I could see that there was a problem using
add-context
in both services.To evaluate regexes you can use https://regexr.com/