301 redirect to "main" domain

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

 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 ?

You can use Traefik redirectregex middleware. 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:

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):

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

I added


      - 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 is also in my .rule="Host(https://example.com)

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

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

Easiest to put it on another one :sweat_smile:

You can use Traefik redirectregex middleware. 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

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