301 redirect using kubernetes

Hello,
I would like to do a 301 redirect from a path that doesn't exist http://mydomain.com/noexistpath to a path that does exist http://mydomain.com
that is to say:
http://mydomain.com/noexistpath ---> http://mydomain.com

I am using a GKE and Traffik V2.4.8 cluster (helm chart)

For this I implemented: a middleware and an ingressroute

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: app-redirect
spec:
  redirectRegex:
    regex: ^https://mydomain.com/noexistpath/(.*)
    replacement: https://mydomain.com/${1}
    permanent: true

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: app-backend-v2
spec:
  routes:
  - kind: Rule
    match: Host(`mydomain.com`)
    services:
    - name: app-backend-v2
      port: 80

  - kind: Rule
    match: Host(`mydomain.com`) && PathPrefix(`/noexistpath`)
    services:
    - name: app-backend-v2
      port: 80
    middlewares:
    - name: app-redirect

Redirection does not work
thank you in advance for your help

Hello @harunaya

The middleware seems to be correct. I think the issue is related to the name of the middleware that has been assigned to the Ingressroute.

Once your created the middleware, its name is built according that convention:

<namespace>-<middleware-name>@kubernetescrd

You haven't specified the namespace so I believe that it would be created in the default namespace, and the name will be the following:

default-app-redirect@kubernetescrd

Would you please update the configuration and change the name to

middlewares:
        - name: default-app-redirect@kubernetescrd

Let us know,
Thank you, Jakub

Hello,
Thank you for your reply.
I create the middleware in a namespace, after the creation of the middleware, on the traeffik dashboard I have the middlware in the form france-app-redirect @ kubernetescrd
I called him with this method, but still the same error:
404 This page could not be found

Would you please share your configuration and the logs generated by Traefik?

Seems that it is a configuration issue because I was using with success the examples you have provided.

Thank you for your reply.
Here is the configuration of my manifests

For the logs I have no logs on this subject

apiVersion: v1
kind: Service
metadata:
  labels:
    app: purevoyance-backend-v2
    team: frontend
  name: purevoyance-backend-v2
spec:
  ports:
  - name: purevoyance-backend-v2
    port: 80
    targetPort: 80
  selector:
    app: purevoyance-backend-v2
  type: ClusterIP

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: purevoyance-backend-v2
    team: frontend
  name: purevoyance-backend-v2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: purevoyance-backend-v2
  template:
    metadata:
      labels:
        app: purevoyance-backend-v2
        team: frontend
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: purevoyance-backend-v2
        ports:
        - containerPort: 80
          name: backend
      enableServiceLinks: false

---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: purevoyance-backend-v2-cert
spec:
  commonName: mycompagny.com
  dnsNames:
  - mycompagny.com
  issuerRef:
    kind: ClusterIssuer
    name: letsencrypt-prod
  secretName: purevoyance-backend-v2-secret

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  labels:
    app: purevoyance-backend-v2
    team: frontend
  name: purevoyance-backend-v2
spec:
  entryPoints:
  - websecure
  routes:
  - kind: Rule
    match: Host(`mycompagny.com`)
    services:
    - name: purevoyance-backend-v2
      port: 80
  - kind: Rule
    match: Host(`mycompagny.com`) && PathPrefix(`/ask`)
    middlewares:
    - name: app-redirect
    services:
    - name: purevoyance-backend-v2
      port: 80
  tls:
    secretName: purevoyance-backend-v2-secret

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  labels:
    app: purevoyance-backend-v2
    team: frontend
  name: purevoyance-backend-v2-http
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    match: Host(`mycompagny.com`)
    middlewares:
    - name: purevoyance-backend-v2-https
    services:
    - name: purevoyance-backend-v2
      port: 80
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: app-redirect
spec:
  redirectRegex:
    permanent: true
    regex: ^https://mycompagny.com/ask/(.*)
    replacement: https://mycompagny.com/${1}
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: purevoyance-backend-v2-https
spec:
  redirectScheme:
    permanent: true
    scheme: https

Thanks for sharing your configuration. I did a few tests using your configuration and requests have been correctly redirected. I can't find anything that should cause the issue. Would you please let us know how to test it? What HTTP response got?

Here is the code that I prepared for testing purposes:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: whoami-crd-https
spec:
  entryPoints:
    - websecure
  routes:
    - kind: Rule
      match: Host(`whoami.127.0.0.1.nip.io`)
      services:
        - kind: Service
          name: whoami-svc
          port: 80
    - kind: Rule
      match: Host(`whoami.127.0.0.1.nip.io`) && PathPrefix(`/none`)
      services:
        - kind: Service
          name: whoami-svc
          port: 80
      middlewares:
        - name: app-redirect
  tls:
    certResolver: le
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: app-redirect
spec:
  redirectRegex:
    regex: ^https://whoami.127.0.0.1.nip.io/none/(.*)
    replacement: https://whoami.127.0.0.1.nip.io/${1}
    permanent: true

Hello,

thank you for your reply
I do a test, the middleware works, I think my problem is this && PathPrefix (/ none)

With this configuration.
When I type on my mycompagny.com browser the redirect is made to mydomain.com/blog.
When I add Host(mycompagny.com) && PathPrefix (/ none)
I type mycompagny.com/none I get an error: 404 Not Found nginx / 1.19.10

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: purevoyance-backend-v2
spec:
  routes:
  - kind: Rule
    match: Host(`mycompagny.com`)
    services:
    - kind: Service
      name: purevoyance-backend-v2
      port: 80
    middlewares:
    - name: app-redirect

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: app-redirect
spec:
  redirectRegex:
    regex: ^https://mycompagny.com/(.*)
    replacement: https://mydomain.com/blog/${1}
    permanent: true