# Traefik redirectregex via docker copmpose file

**URL:** <https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941>\
**Category:** Traefik v2\
**Tags:** docker-swarm\
**Created:** [March 16, 2023, 1:55pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941 "2023-03-16T13:55:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![epifanio](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/epifanio/32/6824_2.png) [@epifanio](https://community.traefik.io/u/epifanio)\
**Post date:** [March 16, 2023, 1:55pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941/1 "2023-03-16T13:55:35Z")

</div>

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/](https://nbs2.csw.met.no/) -- I should be redirected to [https://nbs.csw.met.no/](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!

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [March 16, 2023, 10:21pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941/2 "2023-03-16T22:21:07Z")

</div>

You need to define a `router` that matches with `rule` and assign the middleware to it. See this [simple example](https://github.com/bluepuma77/traefik-best-practice/tree/main/docker-traefik-dashboard-letsencrypt).

---

<div class="post-metadata">

**Author:** ![epifanio](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/epifanio/32/6824_2.png) [@epifanio](https://community.traefik.io/u/epifanio)\
**Post date:** [March 17, 2023, 2:54pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941/3 "2023-03-17T14:54:17Z")

</div>

Thank you so much! adding the router line fixes the issue 🙂

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:

```auto
        - "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](https://dns/opensearch)?  
will be redirected to:  
[https://dns/csw?mode=opensearch&](https://dns/csw?mode=opensearch&)

but instead it returns:

[https://dns/csw?mode=opensearch&](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](https://regex101.com/r/58sIgx/2) and I get the same issue

---

<div class="post-metadata">

**Author:** ![epifanio](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/epifanio/32/6824_2.png) [@epifanio](https://community.traefik.io/u/epifanio)\
**Post date:** [March 17, 2023, 5:48pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941/4 "2023-03-17T17:48:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [March 20, 2023, 5:48pm UTC](https://community.traefik.io/t/traefik-redirectregex-via-docker-copmpose-file/17941/5 "2023-03-20T17:48:55Z")

</div>

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