Traefik redirectregex via docker copmpose file

Hi,

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

  labels:
    - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://nbs2.csw.met.no/(.*)"
    - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://nbs.csw.met.no/$${1}"

I am simply testing the regex/redirect code - so that if I open any URL that starts with https://nbs2.csw.met.no/ -- I should be redirected to https://nbs.csw.met.no/

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:

https://dns/opensearch?
will be redirected to:
https://dns/csw?mode=opensearch&

but instead it returns:

https://dns/csw?mode=opensearch&?

(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.