# 301 redirect to "main" domain

**URL:** <https://community.traefik.io/t/301-redirect-to-main-domain/16547>\
**Category:** Traefik v2\
**Tags:** middleware\
**Created:** [November 22, 2022, 5:54pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547 "2022-11-22T17:54:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnooblet](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mnooblet/32/1975_2.png) [@mnooblet](https://community.traefik.io/u/mnooblet)\
**Post date:** [November 22, 2022, 5:54pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/1 "2022-11-22T17:54:53Z")

</div>

I would like to set a "main" domain where all requests get 301 if it hits another domain.

```auto
 myapp:
    image: registry.whatever.com/apps/app:e02300b
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.myapp.rule=Host(`myapp.at`) || Host(`www.myapp.at`) || Host(`mysameapp.at`) || Host(`www.mysameapp.at`) || Host(`myapp.com`) || Host(`www.myapp.com`) || Host(`mysameapp.com`) || Host(`www.mysameapp.com`)"
      - "traefik.http.routers.myapp.entrypoints=websecure"
      - "traefik.http.routers.myapp.tls=true"
      - "traefik.http.routers.myapp.tls.certresolver=myresolver"
      - "traefik.http.services.myapp.loadbalancer.server.port=8044"

```

How can I configure a redirect to the first Host('myapp.at') for all domains ?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [November 22, 2022, 6:28pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/2 "2022-11-22T18:28:40Z")

</div>

You can use Traefik [redirectregex middleware](https://doc.traefik.io/traefik/middlewares/http/redirectregex/#redirectregex). I just created a separate service to redirect everything with prio=1, meaning the lowest one, if no other domain or path is matched. The service in it is not really used, you can copy the middleware to another container:

```auto
services:
  catchall:
    image: traefik/whoami:v1.8
    hostname: catchall
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.catchall.entrypoints=websecure
      - traefik.http.routers.catchall.rule=HostRegexp(`{host:.+}`)
      - traefik.http.routers.catchall.priority=1
      - traefik.http.routers.catchall.tls.certresolver=myresolver
      - traefik.http.services.catchall.loadbalancer.server.port=80
      - traefik.http.routers.catchall.middlewares=redirectall
      - traefik.http.middlewares.redirectall.redirectregex.regex=.*
      - traefik.http.middlewares.redirectall.redirectregex.replacement=https://example.com

```

Note there is a caveat: because of the non-fixed hostname in my example, Traefik in general doesn't generate new LetsEncrypt certificates for this service. So you would need to use own or LE generated wildcard certificates (with DNS challenge) for this to work under TLS/SSL.

Or you just list all domains in the rule (they still need valid DNS entries for LE):

```auto
  - traefik.http.routers.catchall.rule=Host(`red.example.com`) || Host(`green.blue.example.com`)

```

---

<div class="post-metadata">

**Author:** ![mnooblet](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mnooblet/32/1975_2.png) [@mnooblet](https://community.traefik.io/u/mnooblet)\
**Post date:** [November 23, 2022, 12:59am UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/3 "2022-11-23T00:59:30Z")

</div>

I added

```auto

      - traefik.http.routers.catchall.middlewares=redirectall
      - traefik.http.middlewares.redirectall.redirectregex.regex=.*
      - traefik.http.middlewares.redirectall.redirectregex.replacement=https://example.com

```

to my existing config, it redirects all domains but I'm getting an infinite redirect loop, because in this case [https://example.com](https://example.com) is also in my .rule="Host([https://example.com](https://example.com))

My guess is it would work if I could exclude one domain and not match against all domains with the regex.

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [November 23, 2022, 5:51pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/4 "2022-11-23T17:51:59Z")

</div>

Yes. If you put the middleware on your [rule=example.com](http://rule=example.com) container (or single catch all container), then you need to exclude it.

Easiest to put it on another one 😅

---

<div class="post-metadata">

**Author:** ![JoelGregory11](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JoelGregory11](https://community.traefik.io/u/JoelGregory11)\
**Post date:** [December 1, 2022, 12:09pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/5 "2022-12-01T12:09:11Z")

</div>

You can use Traefik [redirectregex middleware](https://doc.traefik.io/traefik/middlewares/http/redirectregex/#redirectregex). I just created a separate service to redirect everything with prio=1, meaning the lowest one, if no other domain or path is matched. The service in it is not really used, you can copy the middleware [to](https://kazmicardetailing.com/about-us/) another container

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [January 10, 2023, 2:01pm UTC](https://community.traefik.io/t/301-redirect-to-main-domain/16547/6 "2023-01-10T14:01:59Z")

</div>

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