# Stripprefix middleware not working as expected

**URL:** <https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854>\
**Category:** Traefik v3 (latest)\
**Tags:** middleware, docker\
**Created:** [August 1, 2024, 9:24pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854 "2024-08-01T21:24:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![d3473r](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/d3473r/32/9132_2.png) [@d3473r](https://community.traefik.io/u/d3473r)\
**Post date:** [August 1, 2024, 9:24pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/1 "2024-08-01T21:24:14Z")

</div>

Hello, I'm trying to replace caddy with traefik as my main reverse-proxy.  
But I'm unable to do a simple PathPrefix / StripPrefix combination like i do in caddy with handle\_path ([https://caddyserver.com/docs/caddyfile/directives/handle\_path](https://caddyserver.com/docs/caddyfile/directives/handle_path))

My compose.yml

```yaml
services:
  traefik:
    image: traefik:v3.1
    command:
      - --api.insecure=true
      - --providers.docker
      - --log.level=DEBUG
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  echo:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo.rule: Host("localhost")

  echo-api:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo-api.rule: Host("localhost") && PathPrefix("/api")
      traefik.http.routers.echo-api.middlewares": "strip-api"
      traefik.http.middlewares.strip-api.stripprefix.prefixes": "/api"

  echo-auth:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo-auth.rule: Host("localhost") && PathPrefix("/auth")

```

Now if i do a `curl http://localhost/auth/123` the request is routed to the echo-auth container, so far so good. But the request that the auth container sees has the `/auth` prefix still attached.

Now i want to remove this prefix for the echo-api container, but as soon as i do this, the requests are no longer routed to the echo-api container, instead they are catched by the echo container.

This is not what I'm expecting from this config, compare this caddy compose:

```auto
services:
  caddy:
    image: caddy:2
    ports:
      - "80:80"
    volumes:
      - "./Caddyfile:/etc/caddy/Caddyfile"
  echo:
    image: mendhak/http-https-echo
  echo-api:
    image: mendhak/http-https-echo
  echo-auth:
    image: mendhak/http-https-echo

```

With the following Caddyfile:

```auto
:80 {
	handle /auth* {
		reverse_proxy echo-auth:8080
	}

	handle_path /api* {
		reverse_proxy echo-api:8080
	}

	reverse_proxy echo:8080
}

```

Here the `handle_path` directive is stripping the `/api` prefix and routes the requests to the echo-api container and **not** to the echo container.

Can you please help me with this problem, as this is an essential config for a client/server deployment with traefik.

Regards

---

<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:** [August 2, 2024, 4:49pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/2 "2024-08-02T16:49:23Z")

</div>

> [@d3473r](#):
>
> Now if i do a `curl http://localhost/auth/123` the request is routed to the echo-auth container, so far so good. But the request that the auth container sees has the `/auth` prefix still attached.

But echo-auth doesn’t have a stripprefix middleware attached.

---

<div class="post-metadata">

**Author:** ![d3473r](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/d3473r/32/9132_2.png) [@d3473r](https://community.traefik.io/u/d3473r)\
**Post date:** [August 2, 2024, 5:10pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/3 "2024-08-02T17:10:27Z")

</div>

Hi, yes i have the `/auth` container as a control group here, This container does not have its prefix stripped.  
Also in the caddy example the prefix is not stripped as i use `handle` and not `handle_path`.

If you test both compose deployments and test them side-by-side you'll see they are the same for `/auth` (Prefix is not stripped)  
But they differ for `/api`.  
With caddy the `/api` prefix will get stripped correctly, but with traefik it will route to the `echo` container instead

---

<div class="post-metadata">

**Author:** ![d3473r](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/d3473r/32/9132_2.png) [@d3473r](https://community.traefik.io/u/d3473r)\
**Post date:** [August 2, 2024, 5:23pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/4 "2024-08-02T17:23:48Z")

</div>

I'll add the results of the caddy vs traefik tests, so you don't have to spin up the example:

## caddy

```auto
curl localhost -> echo -> "/"
curl localhost/auth/test -> echo-auth -> "/auth/test"
curl localhost/api/test -> echo-api -> "/test"

```

## traefik

```auto
curl localhost -> echo -> "/"
curl localhost/auth/test -> echo-auth -> "/auth/test"
curl localhost/api/test -> echo -> "/api/test" < This is unexpected

```

---

<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:** [August 2, 2024, 8:00pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/5 "2024-08-02T20:00:28Z")

</div>

Maybe you need to fix your `"`s.

From the [doc](https://doc.traefik.io/traefik/middlewares/http/stripprefix/):

```auto
# Strip prefix /foobar and /fiibar
labels:
  - "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes=/foobar,/fiibar"

```

---

<div class="post-metadata">

**Author:** ![d3473r](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/d3473r/32/9132_2.png) [@d3473r](https://community.traefik.io/u/d3473r)\
**Post date:** [August 4, 2024, 2:38pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/6 "2024-08-04T14:38:18Z")

</div>

I changed it to:

```auto
echo-api:
  image: mendhak/http-https-echo
  labels:
    - "traefik.http.routers.echo-api.rule=Host(`localhost`) && PathPrefix(`/api`)"
    - "traefik.http.routers.echo-api.middlewares=`strip-api`"
    - "traefik.http.middlewares.strip-api.stripprefix.prefixes=/api"

```

But getting the same results.  
I think its an internal route ordering issue

---

<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:** [August 4, 2024, 5:48pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/7 "2024-08-04T17:48:46Z")

</div>

> [@d3473r](#):
>
> ```auto
> - "traefik.http.routers.echo-api.middlewares=`strip-api`"
> 
> ```

Why backticks in there?

Enable Traefik dashboard and Traefik debug log to check if your config is correctly recognized by Traefik.

---

<div class="post-metadata">

**Author:** ![crazytimmy](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@crazytimmy](https://community.traefik.io/u/crazytimmy)\
**Post date:** [May 9, 2025, 9:01pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/8 "2025-05-09T21:01:26Z")

</div>

@d3473r did you find a solution? I think I have the same issue you have.

---

<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:** [May 10, 2025, 7:16am UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/9 "2025-05-10T07:16:12Z")

</div>

If you have the same problem, then share the same Traefik static and dynamic config, and Docker compose file(s) if used.

---

<div class="post-metadata">

**Author:** ![crazytimmy](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@crazytimmy](https://community.traefik.io/u/crazytimmy)\
**Post date:** [May 10, 2025, 5:46pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/10 "2025-05-10T17:46:03Z")

</div>

I use labels in the compose file.

lldap compose.yaml

```auto
...
    labels:
      - traefik.enable=true
      - traefik.docker.network=reverse_proxy-lldap
      # Middleware
      - traefik.http.middlewares.lldap-stripprefix.stripprefix.prefixes=/lldap
      # Service
      - traefik.http.services.lldap.loadbalancer.server.port=17170
      - traefik.http.services.lldap.loadbalancer.server.scheme=http
      # Internal Rule
      - traefik.http.routers.lldap-internal.rule=Host(`${HOST_IP}`) && PathPrefix(`/lldap`)
      - traefik.http.routers.lldap-internal.entrypoints=websecure
      - traefik.http.routers.lldap-internal.middlewares=lldap-stripprefix
      - traefik.http.routers.lldap-internal.service=lldap
...

```

With this configuration the page loads but is white. Inspecting the page source, it does load some code. URL is [https://ipaddress/lldap](https://ipaddress/lldap)  
If I change the rule by removing the `&& PathPrefix(`/lldap`)` the URL updates to [https://ipaddress/login](https://ipaddress/login) and the page loads fully. Inspecting the page source is identical to before.

It seems if you change the path so that the rule no longer matches the page won't work. Is this correct and functioning as designed? Or perhaps due to the service that's running the page and how it's designed?

I've also tried several other middlewares combinations with no luck.

```auto
Middleware
      - traefik.http.middlewares.lldap-stripprefix.stripprefix.prefixes=/lldap
      - traefik.http.middlewares.lldap-addprefix.addprefix.prefix=/login
      - traefik.http.middlewares.lldap-redirectregex.redirectregex.regex=${HOST_IP}/lldap
      - traefik.http.middlewares.lldap-redirectregex.redirectregex.replacement=${HOST_IP}/login
      - traefik.http.middlewares.lldap-replacepathregex.replacepathregex.regex=/lldap
      - traefik.http.middlewares.lldap-replacepathregex.replacepathregex.replacement=/login

```

---

<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:** [May 10, 2025, 5:50pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/11 "2025-05-10T17:50:18Z")

</div>

Not every web application supports a custom external path (using PathPrefix() and potentially stripPrefix), instead a sub-domain per service is best practice.

It’s [discussed like here](https://community.traefik.io/t/docmost-pathprefix/27164/2) about every other week.

---

<div class="post-metadata">

**Author:** ![crazytimmy](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@crazytimmy](https://community.traefik.io/u/crazytimmy)\
**Post date:** [May 10, 2025, 6:02pm UTC](https://community.traefik.io/t/stripprefix-middleware-not-working-as-expected/23854/12 "2025-05-10T18:02:31Z")

</div>

Gotcha, was just playing around with a new config at home and trying to get some internal things loading through IP rather then through dns. Thanks for the reply.
