Need help with RedirectRegex

I have a primary domain: mainDomain.com , then I have several other secondary domains: prdomain.com, eventdomain.com... What I am trying to do is redirect *.prdomain.com to https://mainDomain.com/pr-marketing and *.eventdomain.com https://mainDomain.com/events
I cannot seem to figure out the correct syntax to get this to work; I need some to help with an example of how I would write this.

What I have right now:

docker-compose.yaml:

version: '3.7'
services:

  mainsite:
    image: my/joomla
    restart: always
    networks:
      - frontend
    volumes:
      - ./html:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik"
      - "traefik.http.middlewares.mainsite-ssl.redirectscheme.scheme=https"
      # http
      - "traefik.http.routers.mainsite-http.rule=Host(`maindomain.com`,`www.maindomain.com`)"
      - "traefik.http.routers.mainsite-http.entrypoints=web"
      - "traefik.http.routers.mainsite-http.middlewares=mainsite-ssl@docker"
      # https
      - "traefik.http.routers.mainsite.rule=Host(`maindomain.com`,`www.maindomain.com`)"
      - "traefik.http.routers.mainsite.middlewares=secureHeader@file"
      - "traefik.http.routers.mainsite.entrypoints=websecure"
      # Lets Encrypt
      - "traefik.http.routers.mainsite.tls=true"
      - "traefik.http.routers.mainsite.tls.certresolver=le-tls"
      #redirects:
      - "traefik.http.middlewares.pr-redirect.redirectregex.regex=^https?://(www[.])?prdomain[.]com.*"
      - "traefik.http.middlewares.pr-redirect.redirectregex.replacement=https://maindomain.com/pr-marketing"
      - "traefik.http.routers.prsite.rule=HostRegexp(`{subdomain:[a-z]+}.prdomain.com`)"
      - "traefik.http.routers.prsite.middlewares=pr-redirect@docker"

networks:
  frontend:
    external:
      name: traefik

Is what I am trying to do not possible?

Hello,

you can try something like that:

version: '3.7'

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

      # Dashboard
      traefik.http.routers.traefik_dash.rule: Host(`dashboard.localhost`)
      traefik.http.routers.traefik_dash.entrypoints: web
      traefik.http.routers.traefik_dash.service: api@internal

      # Global redirect HTTP to HTTPS
      traefik.http.routers.catchall.rule: HostRegexp(`{any:.+}`)
      traefik.http.routers.catchall.entrypoints: web
      traefik.http.routers.catchall.service: api@internal
      traefik.http.routers.catchall.middlewares: tlsredirect

      # Middlewares
      traefik.http.middlewares.tlsredirect.redirectscheme.scheme: https

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

      traefik.http.routers.mainsite.rule: Host(`maindomain.localhost`,`www.maindomain.localhost`)
      traefik.http.routers.mainsite.entrypoints: websecure
      traefik.http.routers.mainsite.tls: true

      # redirects (https://test.prdomain.localhost/)
      traefik.http.routers.prsite.rule: HostRegexp(`{subdomain:[a-z]+}.prdomain.localhost`)
      traefik.http.routers.prsite.middlewares: pr-redirect
      traefik.http.routers.prsite.entrypoints: websecure
      traefik.http.routers.prsite.tls: true
      
      # Middlewares
      traefik.http.middlewares.pr-redirect.redirectregex.regex: ^https://.*\.?prdomain\.localhost.*
      traefik.http.middlewares.pr-redirect.redirectregex.replacement: https://maindomain.localhost/pr-marketing

1 Like

That worked... thank you very much.