Redirect URL with RedirectRegex not working

Hi,

My existing setup: I've two Ingresses ingress-1 and ingress-2 in two different namespaces (namespace1 and namespace2).
ingress-1 listens on abc.domain.com and ingress-2 listens on def.mydomain.com

My Requirement: Whenever I enter https://abc.domain.com in my browser, my browser should redirect to https://def.mydomain.com and process/routes the request via ingress-2.

I tried to configure RedirectRegex middleware on ingress-2 and also added respective host. After this change, my configuration looks like

Middleware:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: redirect
  namespace: namespace2
spec:
  redirectRegex:
    permanent: true
    regex: ^https://abc.domain.com/(.*)
    replacement: https://def.mydomain.com/$1

Ingress-2:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/router.middlewares: namespace2-redirect@kubernetescrd
  labels:
    app.kubernetes.io/instance: myapp
  name: ingress-2
  namespace: namespace2
spec:
  rules:
  - host: def.mydomain.com
    http:
      paths:
      - backend:
          service:
            name: myapp
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific
  - host: abc.domain.com
    http:
      paths:
      - backend:
          service:
            name: myapp
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

Note: I've removed the ingress-1 in namespace1.

Even after these changes, my browser is not redirecting to def.mydomain.com. However, connecting to myapp (as I've configured this on ingress-2 of-course)

Question: Is my requirement (redirect with code 30x) is possible with Traefik proxy (v2.x)?
If so, what is the solution and where I've to configure?
If not, what is the alternative solution for this?

Hi @kmskpraveen,
Thanks for your interest in Traefik.

As you said and did, having both ingresses with the same rule does not guaranty the router used while forwarding the request. So removing the ingress-1 is good to me.
On my side, I managed to make the redirection working without TLS and in default namespace only.
Did you see the issue with simpler setup as I did?
Could you provide your traefik configuration and your traefik version?

Thanks,
Maxence

Hi @moutoum
Thank for the reply :slight_smile:

I'm using default configurations from official helm chart and v2.7.0. Only, I installed Traefik as NodePort in our K8s cluster and configured AWS ALB on top of it.

May I know is your browser automatically redirecting the page to new domain (Ingress) from your setup?

Thanks,
Praveen.

Hi @kmskpraveen,

Yes my browser follows the redirection as expected. Here is my setup:

traefik.yml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: traefik
  labels:
    app: traefik

spec:
  replicas: 1
  selector:
    matchLabels:
      app: traefik
  template:
    metadata:
      labels:
        app: traefik
    spec:
      serviceAccountName: traefik-ingress-controller
      containers:
        - name: traefik
          image: traefik:v2.7
          args:
            - --api.insecure
            - --log.level=DEBUG
            - --entrypoints.web.address=:80
            - --providers.kubernetescrd
            - --providers.kubernetesingress
          ports:
            - name: web
              containerPort: 80
            - name: dashboard
              containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: traefik-dashboard-service

spec:
  type: LoadBalancer
  ports:
    - port: 8080
      targetPort: dashboard
  selector:
    app: traefik
---
apiVersion: v1
kind: Service
metadata:
  name: traefik-web-service

spec:
  type: LoadBalancer
  ports:
    - targetPort: web
      port: 80
  selector:
    app: traefik

whoami.yml

kind: Deployment
apiVersion: apps/v1
metadata:
  name: whoami
  labels:
    app: whoami

spec:
  replicas: 1
  selector:
    matchLabels:
      app: whoami
  template:
    metadata:
      labels:
        app: whoami
    spec:
      containers:
        - name: whoami
          image: traefik/whoami
          ports:
            - name: web
              containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: whoami

spec:
  ports:
    - port: 80
      targetPort: web
  selector:
    app: whoami
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    traefik.ingress.kubernetes.io/router.middlewares: default-redirect@kubernetescrd
  name: my-web-ingress
spec:
  rules:
  - host: match2.example.com
    http:
      paths:
      - backend:
          service:
            name: whoami
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific
  - host: match.example.com
    http:
      paths:
      - backend:
          service:
            name: whoami
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: redirect
spec:
  redirectRegex:
    permanent: true
    regex: ^http://match.example.com/(.*)
    replacement: http://match2.example.com/$1
1 Like

Thanks @moutoum
This is working. I had to replace https to http in my Redirect middleware :slight_smile:

1 Like

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