Traefik redirectregex via docker copmpose file


I am trying to use a redirectregex in Traefik (traefik:v2.9.1 ) - my container has the following labels:

    - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^*)"
    - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=$${1}"

I am simply testing the regex/redirect code - so that if I open any URL that starts with -- I should be redirected to

but seems my traefik instance is ignoring those instructions - can't find anything in the logs - thanks for any help!

You need to define a router that matches with rule and assign the middleware to it. See this simple example.

Thank you so much! adding the router line fixes the issue :slight_smile:

I will now have to deal with my regex rule .. fighting to get a question mark out of the URL
but it is probably an issue with regex itself and not traefik

my rules are:

        - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=https://dns/opensearch?"
        - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://dns/csw?mode=opensearch&"
       - "traefik.http.routers.adc2-https.middlewares=test-redirectregex"

so that an URL like:

will be redirected to:

but instead it returns:


(note the question mark at the end)

I tried on the playground suggested in the traefik documentation regex101: build, test, and debug regex and I get the same issue

got it to work adding some escape and making sure to use single quote in the yaml file - thanks @bluepuma77 for your help!!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.