Redirection and Routing Rules Issue with Traefik for Multiple Routes

Hello, I'm facing an issue with configuring Traefik to handle redirection and routing rules across multiple containers. I have two applications hosted on different Docker containers and accessed via Traefik:

  • Application A: Located on the main domain (example.com). I need to redirect requests to the root path (/) to another subdomain (web.example.com) for handling user login. However, after users authenticate, they should be able to access specific paths like /app and /callback on example.com without being redirected again.
  • Application B: Hosted on web.example.com, this application handles the authentication process and other UI elements.}

I'm using Traefik v2.x, and I attempted to set up the rules as follows:

# Configuration for Application A on `example.com`
labels:
  - "traefik.enable=true"
  - "traefik.http.routers.app-a.rule=Host(`example.com`) && (PathPrefix(`/callback`) || PathPrefix(`/app`))"
  - "traefik.http.routers.app-a.entrypoints=websecure"
  - "traefik.http.routers.app-a.tls=true"
  - "traefik.http.routers.app-a.tls.certresolver=myresolver"
  - "traefik.http.services.app-a.loadbalancer.server.port=4000"

# Configuration for Application B on `web.example.com`
labels:
  - "traefik.enable=true"
  - "traefik.http.routers.app-b.rule=Host(`web.example.com`)"
  - "traefik.http.routers.app-b.entrypoints=websecure"
  - "traefik.http.routers.app-b.tls=true"
  - "traefik.http.routers.app-b.tls.certresolver=myresolver"
  - "traefik.http.services.app-b.loadbalancer.server.port=3000"

Issue: The rules work to redirect the root (/) to web.example.com for authentication, but after users authenticate, routes like /app and /callback on example.com also redirect to the subdomain instead of staying on example.com or I get a 404 page not found error because the route may have parameters example.com/callback?code=xyz

Can someone please help me, some plugin to fix this?

Probably create an additional login router, something along those lines:

  - "traefik.http.routers.app-a-redirect.rule=Host(`example.com`) && (Path(`/`)"
  - "traefik.http.routers.app-a-redirect.entrypoints=websecure"
  - "traefik.http.routers.app-a-redirect.tls=true"
  - "traefik.http.routers.app-a-redirect.tls.certresolver=myresolver"
  - "traefik.http.middlewares.app-a-redirect.redirectregex.regex=.*"
  - "traefik.http.middlewares.app-a-redirect.redirectregex.replacement=https://web.example.com"
  - "traefik.http.routers.app-a-redirect.middlewares=app-a-redirect"
  - "traefik.http.routers.app-a-redirect.service=noop@internal"

Then use the main A app without PathPrefix:

traefik.http.routers.app-a.rule=Host(`example.com`)

In general I would recommend to move most of the config to entrypoint to reduce repetition, compare to simple Traefik example:

  - "traefik.http.routers.app-a.rule=Host(`example.com`)"
  - "traefik.http.routers.app-a.entrypoints=websecure"
  - "traefik.http.routers.app-a.tls=true"
  - "traefik.http.routers.app-a.tls.certresolver=myresolver"
  - "traefik.http.services.app-a.loadbalancer.server.port=4000"

--->

  - "traefik.http.routers.app-a.rule=Host(`example.com`)"
  - "traefik.http.services.app-a.loadbalancer.server.port=4000"

And move to Traefik latest v3.

Hi @bluepuma77 thanks for your reply.

Look this is my setup for redirection:

domain-web-redirect:
    image: traefik/whoami  
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.domain-web-redirect.rule=Host(`domain.com.co`) && Path(`/`)"
      - "traefik.http.routers.domain-web-redirect.entrypoints=websecure"
      - "traefik.http.routers.domain-web-redirect.tls=true"
      - "traefik.http.routers.domain-web-redirect.tls.certresolver=myresolver"
      - "traefik.http.middlewares.domain-web-redirect.redirectregex.regex=^https://domain[.]com[.]co/$"
      - "traefik.http.middlewares.domain-web-redirect.redirectregex.permanent=true"
      - "traefik.http.middlewares.domain-web-redirect.redirectregex.replacement=https://web.domain.com.co"
      - "traefik.http.routers.domain-web-redirect.middlewares=domain-web-redirect"
      - "traefik.http.routers.domain-web-redirect.service=noop@internal"
    networks:
      - domain-net

I am trying to make the redirection only apply when the url is https://domain.com.co/ only in this case it would be redirected to https://web.domain.com.co but it does not work.

This is my regular expression:

^https://domain[.]com[.]co/$

Do you know where I can see examples of this type of redirects?

In my docker-compose I have the basic configurations for the services to respond to the domains and map to the ports.

Thanks in advance.

Why not use the catch-all regex I supplied? First the router rule is matched, only then the redirect middleware is applied.

Hi @bluepuma77, thank you very much for your suggestion. I implemented your recommendation along with some other details I had internally in the way my apps worked (there was some internal redirection that I hadn't noticed) and it worked perfectly. I was able to achieve the desired routing behavior. Thanks again for your help, I really appreciate it! :pray:

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