Redirect example.com -> subdomain.example.com

Hi all,
I recently switched to Traefik using Docker and It's been a breeze.

The only thing I have yet to figure out is how I would go about directing my main domain name to a subdomain.
I tried setting an HTTP redirect middleware without success (this is from the docker-compose.yml of the Traefik container:

    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.domain.entrypoints=websecure"
      - "traefik.http.routers.domain.rule=Host(`example.com`)"
      - "traefik.http.routers.domain.service=noop@internal"
      - "traefik.http.routers.domain.middlewares=subdomain-redirect"
      - 'traefik.http.middlewares.subdomain-redirect.redirectregex.regex=^http://example.com/(.*)'
      - 'traefik.http.middlewares.subdomain-redirect.redirectregex.replacement=http://subdomain.example.com/$${1}'
      - "traefik.http.middlewares.subdomain-redirect.redirectregex.permanent=true"

I realise there are plently of similar questions around the web, but I could not figure it out...
I've tried several variations of the code above without success.

How should I go about redirecting my main domain to a subdomain?
Any pointers would be greatly appreciated.

Thank you!

Hi,

I managed to solve this like so:

  labels:
      - "traefik.enable=true"
      - "traefik.http.routers.domain.entrypoints=websecure"
      - "traefik.http.routers.domain.rule=Host(`example.com`)"
      - "traefik.http.routers.domain.tls.certresolver=certresolver"
      - "traefik.http.routers.domain.middlewares=domain"
      - 'traefik.http.middlewares.domain.redirectregex.regex=^https://example.com/(.*)'
      - 'traefik.http.middlewares.domain.redirectregex.replacement=https://subdomain.example.com/$${1}'
      - "traefik.http.middlewares.domain.redirectregex.permanent=true"`

Hope this helps!

1 Like

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