Redirect if route doesn't exist

I would like to redirect to another domain/subdomain if a route does not match any of the existing traefik routes.

IE. If I have an app running connected with traefik at domain1.com/whoami
And I hit a non-existing route domain1.com/test is there a way I can forward the request to domain2.com/test?

I took a look at defining custom error pages, but redirecting is what I'm after.
So far I've tried using a defaultRule as well as middleware redirects like so:

      labels:
        - traefik.http.middlewares.redirect-other.redirectregex.permanent=true
        - traefik.http.middlewares.redirect-other.redirectregex.regex=^http(s*)://localhost/(.*)
        - traefik.http.middlewares.redirect-other.redirectregex.replacement=https://test.${DOMAIN}/$${1}

This creates valid middleware which I'm able to see on the dashboard, but hitting localhost/test
just gives me a "404 page not found".

Any help would be appreciated

Thanks

Hello,

You can do something like that:

version: '3.7'

services:

  traefik:
    image: traefik:v2.0.5
    command: 
      - --api
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --providers.docker=true
      - --providers.docker.exposedByDefault=false
      - --log.level=INFO
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      traefik.enable: true

      traefik.http.routers.api.rule: Host(`zone1.dashboard.localhost`)
      traefik.http.routers.api.entrypoints: web
      traefik.http.routers.api.service: api@internal
  
  whoami1:
    image: containous/whoami
    labels:
      traefik.enable: true

      traefik.http.routers.whoami1.rule: Host(`domain1.localhost`) && PathPrefix(`/whoami`)
      traefik.http.routers.whoami1.entrypoints: websecure
      traefik.http.routers.whoami1.tls: true

      traefik.http.routers.catch.rule: Host(`domain1.localhost`)
      traefik.http.routers.catch.entrypoints: websecure
      traefik.http.routers.catch.tls: true
      traefik.http.routers.catch.middlewares: redirect_other
      
      traefik.http.middlewares.redirect_other.redirectregex.regex: ^https?://domain1.localhost/(.*)
      traefik.http.middlewares.redirect_other.redirectregex.replacement: https://domain2.localhost/$${1}
      traefik.http.middlewares.redirect_other.redirectregex.permanent: true

  whoami2:
    image: containous/whoami
    labels:
      traefik.enable: true

      traefik.http.routers.whoami2.rule: Host(`domain2.localhost`)
      traefik.http.routers.whoami2.entrypoints: websecure
      traefik.http.routers.whoami2.tls: true
curl -k domain1.localhost/whoami
# whoami1
curl -k -L domain1.localhost/test
# redirect to domain1.localhost/test in whoami2

[Edit]
The catch middleware and tls were required to work. Adding this to the first service worked!

        - traefik.http.routers.catch.rule=Host(`domain1.localhost`)
        - traefik.http.routers.catch.entrypoints=websecure
        - traefik.http.routers.catch.tls=true
        - traefik.http.routers.catch.middlewares=redirect_other

Hi @Idez thanks for your response.
I'm having trouble getting your example to work.
With the following I have two whoamis running on domain1.localhost/whoami and domain2.localhost/test
but visiting domain1.localhost/test just gives a 404 and there's no traefik log running in log level debug.

version: "3.7"

services:
  whoami:
    container_name: "whoami"
    image: "containous/whoami"
    deploy:
      labels:
        - traefik.enable=true  # Explicitly tell Traefik to expose this container
        - traefik.http.services.whoami-service.loadbalancer.server.port=80 # expose the port the app is running on
        # http
        - traefik.http.routers.whoami.entrypoints=web
        - traefik.http.routers.whoami.rule=Host(`domain1.localhost`) && PathPrefix(`/whoami`)
        # middleware redirect http -> https
        - traefik.http.routers.whoami.middlewares=redirect@file
        # https
        - traefik.http.routers.whoami-tls.rule=Host(`domain1.localhost`) && PathPrefix(`/whoami`)
        - traefik.http.routers.whoami-tls.entrypoints=websecure
        - traefik.http.routers.whoami-tls.tls=true

        - traefik.http.routers.whoami.rule=Host(`domain1.localhost`)
        - traefik.http.routers.whoami.middlewares=redirect_other
        
        - traefik.http.middlewares.redirect_other.redirectregex.regex=^https?://domain1.localhost/(.*)
        - traefik.http.middlewares.redirect_other.redirectregex.replacement=https://domain2.localhost/$${1}
        - traefik.http.middlewares.redirect_other.redirectregex.permanent=true

    networks:
      - traefik-public
      
networks:
  traefik-public:
    external: true
version: "3.7"

services:
  whoami2:
    container_name: "whoami2"
    image: "containous/whoami"
    deploy:
      labels:
        - traefik.enable=true  # Explicitly tell Traefik to expose this container
        - traefik.http.services.whoami2-service.loadbalancer.server.port=80 # expose the port the app is running on
        # http
        - traefik.http.routers.whoami2.entrypoints=web
        - traefik.http.routers.whoami2.rule=Host(`domain2.localhost`) && PathPrefix(`/test`)
        # middleware redirect http -> https
        - traefik.http.routers.whoami2.middlewares=redirect@file
        # https
        - traefik.http.routers.whoami2-tls.rule=Host(`domain2.localhost`) && PathPrefix(`/test`)
        - traefik.http.routers.whoami2-tls.entrypoints=websecure
        - traefik.http.routers.whoami2-tls.tls=true

    networks:
      - traefik-public
      
networks:
  traefik-public:
    external: true