# Middleware to add the "/" if needed

**URL:** <https://community.traefik.io/t/middleware-to-add-the-if-needed/1895>\
**Category:** Traefik v2\
**Tags:** docker, docker-swarm, middleware\
**Created:** [September 26, 2019, 12:53am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895 "2019-09-26T00:53:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 12:53am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/1 "2019-09-26T00:53:38Z")

</div>

To complement [defaultRule to get the service name without the stack](https://community.containo.us/t/defaultrule-to-get-the-service-name-without-the-stack/1847/2)  
I am trying to make a common middleware that would strip the path prefix

```auto
        - traefik.http.middlewares.strip-prefix.stripprefixregex.regex=/[a-z0-9_]+

```

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 also tried (updated based on comment by @zespri

```auto
        - traefik.http.middlewares.strip-prefix.replacepathregex.regex=^/[a-z0-9_]+
        - traefik.http.middlewares.strip-prefix.replacepathregex.replacement=/

```

I also did the following which uses a `redirectregex` also no luck

```
    - traefik.http.middlewares.strip-prefix.chain.middlewares=strip-prefix-1,strip-prefix-2
    - traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9_]+)$$
    - traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=$${1}/
    - traefik.http.middlewares.strip-prefix-1.redirectregex.permanent=true
    - traefik.http.middlewares.strip-prefix-2.stripprefixregex.regex=/[a-z0-9_]+
```

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [September 26, 2019, 1:58am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/2 "2019-09-26T01:58:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 2:00am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/3 "2019-09-26T02:00:26Z")

</div>

Thanks for the catch, but that didn't work either ☹

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [September 26, 2019, 2:07am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/4 "2019-09-26T02:07:49Z")

</div>

Could you try something like that:

```auto
- traefik.http.middlewares.strip-prefix.replacepathregex.regex=^(/[a-z0-9_]+)
- traefik.http.middlewares.strip-prefix.replacepathregex.replacement=${1}/

```

[https://docs.traefik.io/v2.0/middlewares/replacepathregex/](https://docs.traefik.io/v2.0/middlewares/replacepathregex/)

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 2:10am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/5 "2019-09-26T02:10:50Z")

</div>

won't that preserve the prefix? I'm trying to remove the prefix that gets sent otherwise we'd have `/portainer/portainer/`

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 2:36am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/6 "2019-09-26T02:36:00Z")

</div>

Here's the solution

```
    - traefik.http.middlewares.strip-prefix.chain.middlewares=strip-prefix-1,strip-prefix-2
    - traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9_]+)$$
    - traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=$${1}/
    - traefik.http.middlewares.strip-prefix-1.redirectregex.permanent=true
    - traefik.http.middlewares.strip-prefix-2.stripprefixregex.regex=/[a-z0-9_]+
```

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [September 26, 2019, 2:37am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/7 "2019-09-26T02:37:45Z")

</div>

Now we just need to figure out ssl passthrough from that other thread 😉

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 2:42am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/8 "2019-09-26T02:42:17Z")

</div>

> [@zespri](#):
>
> ssl passthrough

I already solved that one in [Trajano base Docker swarm stacks - Traefik v2 (latest) - Traefik Labs Community Forum](https://community.containo.us/t/trajano-base-docker-swarm-stacks/1899)

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [September 26, 2019, 2:43am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/9 "2019-09-26T02:43:02Z")

</div>

Oh right! I saw that but did not realize it was related.

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [September 26, 2019, 3:33am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/10 "2019-09-26T03:33:26Z")

</div>

@trajano why do you have both main and sans the same?

```auto
        - traefik.tcp.routers.intranet.tls.domains[0].main=i.trajano.net
        - traefik.tcp.routers.intranet.tls.domains[0].sans=i.trajano.net

```

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [September 26, 2019, 3:44am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/11 "2019-09-26T03:44:05Z")

</div>

@zespri wrong topic to ask that question.

---

<div class="post-metadata">

**Author:** ![ipss](https://avatars.discourse-cdn.com/v4/letter/i/c68b51/32.png) [@ipss](https://community.traefik.io/u/ipss)\
**Post date:** [January 1, 2020, 1:50pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/12 "2020-01-01T13:50:11Z")

</div>

That is the same as in the opening. I am also looking for solution for apps like portainer.

---

<div class="post-metadata">

**Author:** ![pascalandy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/pascalandy/32/289_2.png) [@pascalandy](https://community.traefik.io/u/pascalandy)\
**Post date:** [January 16, 2020, 12:52am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/13 "2020-01-16T00:52:35Z")

</div>

> [@trajano](#):
>
> Here's the solution
> 
> ```auto
> - traefik.http.middlewares.strip-prefix.chain.middlewares=strip-prefix-1,strip-prefix-2
> - traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9_]+)$
> - traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=${1}/
> - traefik.http.middlewares.strip-prefix-1.redirectregex.permanent=true
> - traefik.http.middlewares.strip-prefix-2.stripprefixregex.regex=/[a-z0-9_]+
> 
> ```

@trajano you freaking rock man!! You're my regex God 🤩 😇 🥰

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [January 16, 2020, 7:38pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/14 "2020-01-16T19:38:50Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![pascalandy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/pascalandy/32/289_2.png) [@pascalandy](https://community.traefik.io/u/pascalandy)\
**Post date:** [January 17, 2020, 10:36pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/15 "2020-01-17T22:36:50Z")

</div>

@trajano do you happen to know how to do the opposite? Meaning, to remove a slash if there is one

 ![Screen Shot 2020-01-17 at 5.35.45 PM](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd7007143833ed7857e96e27e3486e7377e0a918.jpeg) ![Screen Shot 2020-01-17 at 5.35.27 PM](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/8/831f992020dee9062364b0afa87082538c41b931.jpeg)

I'm running

```auto
version: "3.3"

services:

  hello:
    image: hashicorp/http-echo
    container_name: hello
    hostname: hello
    restart: unless-stopped
    command: "-listen=':5001' -text='Hello Pascal'"

    labels:
      #### core configs
      - "traefik.enable=true"
      - "traefik.http.routers.hello.rule=Host(`devkiwi.club`) && Path(`/hello`)"
      - "traefik.http.services.hello.loadbalancer.server.port=5001"
      #### set TLS (https)
      - "traefik.http.routers.hello.entrypoints=websecure"
      - "traefik.http.routers.hello.tls=true"
      - "traefik.http.routers.hello.tls.certresolver=leresolver"

      #### https://twitter.com/askpascalandy

```

---

<div class="post-metadata">

**Author:** ![trajano](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trajano/32/247_2.png) [@trajano](https://community.traefik.io/u/trajano)\
**Post date:** [February 17, 2020, 2:22pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/16 "2020-02-17T14:22:59Z")

</div>

Just reversed it here but I haven't tested it

> [@pascalandy](#):
>
> ```auto
> - traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9_]+)/$
> - traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=${1}
> 
> ```

basically the position of the `/` is swapped.

---

<div class="post-metadata">

**Author:** ![pascalandy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/pascalandy/32/289_2.png) [@pascalandy](https://community.traefik.io/u/pascalandy)\
**Post date:** [February 17, 2020, 3:38pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/17 "2020-02-17T15:38:39Z")

</div>

Thanks will test it!!

> [@trajano](#):
>
> - traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9\_]+)/$ - traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=${1}

---

<div class="post-metadata">

**Author:** ![jaworek](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/jaworek/32/1625_2.png) [@jaworek](https://community.traefik.io/u/jaworek)\
**Post date:** [February 24, 2020, 8:01am UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/18 "2020-02-24T08:01:50Z")

</div>

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 exact same issue: [Cannot reacreate Nginx proxy\_pass behavior with Traefik](https://community.containo.us/t/cannot-reacreate-nginx-proxy-pass-behavior-with-traefik/4527)

---

<div class="post-metadata">

**Author:** ![coltenkrauter](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/coltenkrauter/32/3236_2.png) [@coltenkrauter](https://community.traefik.io/u/coltenkrauter)\
**Post date:** [January 26, 2021, 5:47pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/19 "2021-01-26T17:47:32Z")

</div>

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.

```auto
      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

```

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [January 26, 2021, 9:16pm UTC](https://community.traefik.io/t/middleware-to-add-the-if-needed/1895/20 "2021-01-26T21:16:20Z")

</div>


