# Redirect if route doesn't exist

**URL:** <https://community.traefik.io/t/redirect-if-route-doesnt-exist/3154>\
**Category:** Traefik v2\
**Tags:** docker-swarm, middleware, docker\
**Created:** [November 25, 2019, 7:01pm UTC](https://community.traefik.io/t/redirect-if-route-doesnt-exist/3154 "2019-11-25T19:01:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcowell](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bcowell](https://community.traefik.io/u/bcowell)\
**Post date:** [November 25, 2019, 7:01pm UTC](https://community.traefik.io/t/redirect-if-route-doesnt-exist/3154/1 "2019-11-25T19:01:22Z")

</div>

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](http://domain1.com/whoami)  
And I hit a non-existing route [domain1.com/test](http://domain1.com/test) is there a way I can forward the request to [domain2.com/test](http://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:

```auto
      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

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [November 25, 2019, 9:31pm UTC](https://community.traefik.io/t/redirect-if-route-doesnt-exist/3154/2 "2019-11-25T21:31:46Z")

</div>

Hello,

You can do something like that:

```auto
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

```

```auto
curl -k domain1.localhost/whoami
# whoami1

```

```auto
curl -k -L domain1.localhost/test
# redirect to domain1.localhost/test in whoami2

```

---

<div class="post-metadata">

**Author:** ![bcowell](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bcowell](https://community.traefik.io/u/bcowell)\
**Post date:** [November 26, 2019, 4:23pm UTC](https://community.traefik.io/t/redirect-if-route-doesnt-exist/3154/4 "2019-11-26T16:23:38Z")

</div>

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

```auto
        - 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.

```auto
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

```

```auto
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

```
