# Traefik Ingress rewrite-target does not work

**URL:** <https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127>\
**Category:** Traefik v2\
**Tags:** kubernetes-ingress\
**Created:** [January 21, 2022, 9:12am UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127 "2022-01-21T09:12:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cawoodm/32/617_2.png) [@cawoodm](https://community.traefik.io/u/cawoodm)\
**Post date:** [January 21, 2022, 9:12am UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/1 "2022-01-21T09:12:45Z")

</div>

Hello,  
As described [here](https://stackoverflow.com/questions/70789020/traefik-ingress-rewrite-target-does-nothing) and [here](https://community.traefik.io/t/traefik-ingress-with-k3s-rewrite-target-issue/12736) the annotation `***.ingress.kubernetes.io/rewrite-target` in Kubernetes Ingress seems to work fine with NGINX but not with traefik.

Not sure if this is a Traefik or Kubernetes Issue but Traefik Ingress Controller is just ignoring `traefik.ingress.kubernetes.io/rewrite-target`:

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: registry-ingress
  namespace: ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: myhost.com
      http:
        paths:
        - path: /overview
          pathType: Prefix
          backend:
            service:
              name: overview
              port:
                number: 8079

```

Similarly with `path: /foo(.*)` and `traefik.ingress.kubernetes.io/rewrite-target: /$1` no path rewriting is taking place and a 404 is returned.

---

<div class="post-metadata">

**Author:** ![tommoulard](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tommoulard/32/2997_2.png) [@tommoulard](https://community.traefik.io/u/tommoulard)\
**Post date:** [January 21, 2022, 9:27am UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/2 "2022-01-21T09:27:11Z")

</div>

Hello @cawoodm,

Traefik does not support the `traefik.ingress.kubernetes.io/rewrite-target` annotation. See the list of available annotations here: [Kubernetes Ingress - Traefik](https://doc.traefik.io/traefik/routing/providers/kubernetes-ingress/#annotations)

But your goal can be achieved using the [StipPrefix](https://doc.traefik.io/traefik/middlewares/http/stripprefix/) middleware.

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cawoodm/32/617_2.png) [@cawoodm](https://community.traefik.io/u/cawoodm)\
**Post date:** [January 21, 2022, 1:19pm UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/3 "2022-01-21T13:19:35Z")

</div>

Thanks Tom, I looked into that and it seems I have to employ Middleware and Ingress Routers.

Is there no way to configure or "talk to" traefik from an Ingress Resource?

I guess I could create a Middleware but I don't see how to link it to my routing rules which are in an Ingress Resource `kind: Ingress`.

---

<div class="post-metadata">

**Author:** ![tommoulard](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tommoulard/32/2997_2.png) [@tommoulard](https://community.traefik.io/u/tommoulard)\
**Post date:** [January 21, 2022, 1:28pm UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/4 "2022-01-21T13:28:24Z")

</div>

You have access to this annotation on Ingress: `traefik.ingress.kubernetes.io/router.middlewares: my-stripprefix@kubernetescrd`.

Hope it helps 🙂

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cawoodm/32/617_2.png) [@cawoodm](https://community.traefik.io/u/cawoodm)\
**Post date:** [January 21, 2022, 1:42pm UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/5 "2022-01-21T13:42:55Z")

</div>

> [@tommoulard](#):
>
> `traefik.ingress.kubernetes.io/router.middlewares: my-stripprefix@kubernetescrd` .

Unfortunately, as soon as I apply the annotation everything stops working and I just get 404s.

```auto
│ 10.42.0.1 - - [21/Jan/2022:13:40:39 +0000] "GET /overview/ HTTP/2.0" 404 19 "-" "-" 23891 "-" "-" 0ms

```

Also I had to remove the `@kubernetescrd` part as traefik did not like it:

```auto
time="2022-01-21T13:40:00Z" level=error msg="middleware \"mw-admin@kubernetescrd\" does not exist" entryPointName=web routerName=

```

```auto
---
# Middleware
# Strip prefix /overview
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: mw-admin
  namespace: ingress
spec:
  stripPrefix:
    forceSlash: false
    prefixes:
      - /overview
---
# Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: registry-ingress
  namespace: ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/router.middlewares: mw-admin
spec:
  tls:
  - hosts:
    - admin.mydomain.com
    secretName: tls-secret
  rules:
    - host: admin.mydomain.com
      http:
        paths:
        - path: /overview
          pathType: Prefix
          backend:
            service:
              name: overview
              port:
                number: 8079

```

I tried with `forceSlash: false`, `forceSlash: true` and without this line.

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cawoodm/32/617_2.png) [@cawoodm](https://community.traefik.io/u/cawoodm)\
**Post date:** [January 21, 2022, 2:02pm UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/6 "2022-01-21T14:02:50Z")

</div>

OK, thanks to [this post](https://community.traefik.io/t/how-to-configure-middleware-with-kubernetes-ingress-middleware-xyz-does-not-exist/5016) I was able to solve it.  
The namespace must pre prefixed to the annotation and the @ suffix appended!

`traefik.ingress.kubernetes.io/router.middlewares: ingress-mw-admin@kubernetescrd`

---

<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 18, 2022, 8:30am UTC](https://community.traefik.io/t/traefik-ingress-rewrite-target-does-not-work/13127/7 "2022-03-18T08:30:19Z")

</div>

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