However, this causes an issue if I don't put the trailing slash. For example if I put in /whoami2 it keeps it at /whoami2, I want it to redirect to /whoami2/ This is because some apps like Portainer will not work if the path is /portainer but works with /portainer/
I would rather not put in a / at the end explicitly either.
I think it's called ReplacePathRegex, not ReplacePath. Also if you use both stripprefixregex and replacepathregex you should give them different names using strip-prefix for both won't work...
@pascalandy Also note that in yaml different escaping rules applies depending if you surround your string in quotation marks or not. In your original problem there were quotatation marks. So watch for these too
Thanks for the solution! I was going crazy trying to fix it.
I would be nice to have a better solution built in to Traefik to fix this kind of problem and make setup more similar to how Nginx behaves by default.
I had some issues with the other recommended solutions here. My goal was to get the /dashboard path to redirect to /dashboard/ so that the traefik dashboard would be easier to get to.
Here is the solution I ended up with... Note, it isn't a catchall and it is hard coded for the /dashboard path, but it is a good starting point that should be easy to expand with wildcards.
labels:
- traefik.enable=true
# required in swarm, but can be any port number.
- traefik.http.services.api@internal.loadbalancer.server.port=8080
- traefik.http.routers.dashboard.rule=Host(`example.com`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))
# middlewares
- traefik.http.middlewares.dashboard-auth.basicauth.users={{ htpasswd }}
- traefik.http.routers.dashboard.middlewares=dashboard-auth
# Add trailing slash to /dashboard path
- traefik.http.middlewares.dashboard-strip-slash.redirectregex.regex=(^.*\/dashboard$$)
- traefik.http.middlewares.dashboard-strip-slash.redirectregex.replacement=$$1/
- traefik.http.middlewares.dashboard-strip-slash.redirectregex.permanent=false
- traefik.http.routers.dashboard.middlewares=dashboard-strip-slash