Redirect not working

Hi all,

First time working with Traefik configuration, I seek help to implement a redirect in Traefik.

My aim is to have Traefik responds to a http request whose path would be "/myapp_prod" with a redirect to "/myapp_prod/".
(because that's what the backend app do normally)
(or aim could be to modify the redirect from the backend app from "/myapp/" to /myapp_prod/".. )

So if I'm not wrong, I understand that this can be achieved with the redirectRegex Middleware.
I passed lots of time on this, and can't find a solution.

My version of Traefik : 3.2.0 community edition

So when I try :

# Source: chart-myapp/templates/05-ingressroute.yaml
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: recette-prod-chart-myapp-ingressroute
spec:
  entryPoints:
    - "web"
  routes:
    - kind: Rule
      match: "Path(`/myapp_prod`)"
      middlewares:
        - name: recette-prod-redirectregex
          namespace: myapp
      priority: 2
---
# Source: chart-myapp/templates/04-middleware.yaml
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: recette-prod-redirectregex
spec:
  redirectRegex:
    regex: "^/myapp_prod$"
    replacement: "/myapp_prod/"
    permanent: false

=> in traefik dashboard in the corresponding http router recette-prod-chart-myapp-ingressroute, I get "errors the service "myapp-recette-prod-chart-myapp-ingressroute-578274fbd352e7079863@kubernetescrd" does not exist"
and "curl -Lv http://127.0.0.1:30002/myapp_prod" gives me a 404 from traefik

then If I put a dummy service like :

---
# Source: chart-myapp/templates/05-ingressroute.yaml
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: recette-prod-chart-myapp-ingressroute
  labels:
spec:
  entryPoints:
    - "web"
  routes:
    - kind: Rule
      match: "Path(`/myapp_prod`)"
      middlewares:
        - name: recette-prod-redirectregex
          namespace: myapp
      priority: 2
      services:
        - kind: Service
          name: dummy
          namespace: myapp
          passHostHeader: true
          port: 8080
          strategy: RoundRobin
          weight: 1

=> in traefik dashboard, the http router is not created
curl to my app gives me a 404

then if I put my real backend service :

---
# Source: chart-myapp/templates/05-ingressroute.yaml
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: recette-prod-chart-myapp-ingressroute
spec:
  entryPoints:
    - "web"
  routes:
    - kind: Rule
      match: "Path(`/myapp_prod`)"
      middlewares:
        - name: recette-prod-redirectregex
          namespace: myapp
      priority: 2
      services:
        - kind: Service
          name: recette-prod-chart-myapp
          namespace: myapp
          passHostHeader: true
          port: 8080
          strategy: RoundRobin
          weight: 1

=> in traefik dashboard, the http router is created with no error
but curl to my app shows that the request is forwarded to my backend server which responds with a 404
so the redirect is not working

How to create a successfull redirect with traefik ?

Note that the middelware replacePathRegex works ok in the same ingressroute and for the same backend.

I appreciate any help, thank you

The Traefik RedirectRegex doc is using the full URL:

# Redirect with domain replacement
http:
  middlewares:
    test-redirectregex:
      redirectRegex:
        regex: "^http://localhost/(.*)"
        replacement: "http://mydomain/${1}"

Thank you, I was indeed not considering that redirectRegex concanetes scheme+host+path !

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