Best way to globally apply some redirects


We have all our containers running on a subdomain and exposed via ports. Right now for one service I am rewriting from the old subdomain to the new and redirecting the scheme and port to https and whatever port is needed like so:

      - traefik.enable=true
      - traefik.http.routers.web.rule=Host(``) || Host(``)
      - traefik.http.routers.web.entrypoints=my-service
      - traefik.http.middlewares.web-https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.web-https-redirect.redirectscheme.port=8888
      - traefik.http.routers.web.middlewares=web-https-redirect
      -``) || Host(``)
      - traefik.http.middlewares.sub-redirect.redirectregex.regex=^*)
      - traefik.http.middlewares.sub-redirect.redirectregex.replacement=$${1}

How do I do this globally, rather than replicating all these for each exposed container. I did try in the config, but was unsuccessful

Hi @jonny7

You can apply middlewares to an entrypoint. These middlewares are prepended to any a router provide, so would apply to every router using the entrypoint.

I define these middlewares in a file provider or in the traefik service itself so I know they will exist, then use the below option to attach them to the entrypoint.


So I wrote this middleware

    regex = "`^*)`"
    replacement = "`$${1}`"
    permanent = true

And I apply it to a container like:

      - traefik.enable=true
      - traefik.http.routers.testrail.tls=true
      - traefik.http.routers.testrail.entrypoints=testrail
      - traefik.http.routers.testrail.rule=Host(``) || Host(``)
      - traefik.http.routers.testrail.middlewares=sub-name@file

Both URLs are now accessible, but the url doesn't change from to

No backticks required for regex and replacement. As far as I can tell (and do).

1 Like

Ok that fixed the redirect, I did update my regex to this:

    regex = "http(s)?://*)"
    replacement = "${2}"
    permanent = true

and it in regxr it seems to work fine, but I seem to lose the port, when I try to redirect

As an example if I navigate to it becomes

Hi Johnny,

Do you have any other redirects? Command line, environment or router labels?

Hi @cakiwi

Oddly, this seems to be ok now, but http urls won't redirect to https. (Maybe traefik hadn't updated properly when I last replied).

As far as I'm aware, only what you see above and the api router of in the dynamic config:

    rule = "Host(``)"
    entrypoints = ["websecure"]
    middlewares = ["simpleAuth"]
    service = "api@internal"

Everything else seems fine, the port redirects fine, so does the domain name. But I get 404 page not found. Assuming I need a router on the non-tls. Which I added, but it just let me access it over non-https rather than redirect