Multi-domain redirect www to apex using docker

Just documenting a solution here after solving for a client…

Extending off of the solution from @bpj here: Multiple domains on single docker app

Client has multiple domains on the same web app and is using an otherwise standard docker setup.

Now wants to redirect all www versions of the domains to the apex/naked domain rather than having the www remain in the url.

In other words, is hosting:

domaina.com, domainb.com, domainc.com, etc.

and wants:

www.domaina.com to redirect to domaina.com
www.domainb.com to redirect to domainb.com
www.domainc.com to redirect to domainc.com

And needs traefik to provide a secure certificate for it all.


Steps to create

  1. Create a vanilla docker target using wappler with a single domain.
  2. Modify the docker-compose.yml file found in the .wappler/target folder by adding your extra domains to the existing Host() lines. Modify the domain redirect lines as shown below. Keep in mind that when modifying these lines, you are only changing the portion after the “=” as your routers and middlewares lines will be specific to your setup.
  3. Save the yml and redeploy

IMPORTANT SAFETY TIP: Save a copy of your modified yml file because if you ever save the target from within wappler, all your changes will be erased.

Here is an entire docker-compose that works:

version: '3'
services:
  web:
    ports:
      - '3000'
    restart: 'always'
    stdin_open: true
    tty: true
    logging:
      options:
        max-file: '5'
        max-size: '10m'
    build:
      context: '../../../'
      dockerfile: '.wappler/targets/Remove dev/web/Dockerfile'
    labels:
      - 'traefik.enable=true'
      - 'traefik.http.routers.testing2__remove_dev_web.entrypoints=web'
      - 'traefik.http.routers.testing2__remove_dev_web.rule=Host(`domaina.com`,`domainb.com`, `domainc.com`)'
      - 'traefik.http.routers.testing2__remove_dev_web-secure.entrypoints=websecure'
      - 'traefik.http.routers.testing2__remove_dev_web-secure.rule=Host(`domaina.com`,`domainb.com`, `domainc.com`)'
      - 'traefik.http.routers.testing2__remove_dev_web-secure.tls=true'
      - 'traefik.http.routers.testing2__remove_dev_web-secure.tls.certresolver=leresolver'
      - 'traefik.http.services.testing2__remove_dev_web.loadbalancer.server.port=3000'
      - 'traefik.docker.network=wappler-compose_proxy'
      - 'traefik.http.routers.testing2__remove_dev_www_web-secure.entrypoints=websecure'
      - 'traefik.http.routers.testing2__remove_dev_www_web-secure.rule=Host(`www.domaina.com`, `www.domainb.com`, `www.domainc.com`)'
      - 'traefik.http.routers.testing2__remove_dev_www_web-secure.tls=true'
      - 'traefik.http.routers.testing2__remove_dev_www_web-secure.tls.certresolver=leresolver'
      - 'traefik.http.middlewares.testing2__remove_dev_www-redirect.redirectregex.regex=^https?://(?:www\.)?(.+)'
      - 'traefik.http.middlewares.testing2__remove_dev_www-redirect.redirectregex.replacement=https://$${1}'
      - 'traefik.http.middlewares.testing2__remove_dev_www-redirect.redirectregex.permanent=true'
      - 'traefik.http.routers.testing2__remove_dev_www_web-secure.middlewares=testing2__remove_dev_www-redirect'
    networks:
      proxy: ~
volumes:
  db-volume: ~
networks:
  proxy:
    external:
      name: 'wappler-compose_proxy'

The lines that were modified from the original wappler docker-compose file…

The web hosts:

 - 'traefik.http.routers.testing2__remove_dev_web.rule=Host(`domaina.com`,`domainb.com`, `domainc.com`)'

where the apex domains are listed.

Same for secure hosts:

 - 'traefik.http.routers.testing2__remove_dev_web-secure.rule=Host(`domaina.com`,`domainb.com`, `domainc.com`)'

And lastly the www versions:

- 'traefik.http.routers.testing2__remove_dev_www_web-secure.rule=Host(`www.domaina.com`, `www.domainb.com`, `www.domainc.com`)'

And lastly, the redirect of all www to apex:

- 'traefik.http.middlewares.testing2__remove_dev_www-redirect.redirectregex.regex=^https?://(?:www\.)?(.+)'
- 'traefik.http.middlewares.testing2__remove_dev_www-redirect.redirectregex.replacement=https://$${1}'
Community Page
Last updated: