!-->

How to Redirect a Domain to Another Domain with Traefik

In this blog post, you will learn how to use Traefik to redirect traffic from one domain to another temporarily.

3 min read
How to Redirect a Domain to Another Domain with Traefik
Photo de Yevhen Sukhenko on Pexels .

One month ago, I submitted a business plan as part of the Defi OSEntreprendre , using a new domain that I had not yet created a website for. I was worried that if a member of the jury clicked on one of my links, they would encounter a 404 error page , which would not be great for my project's credibility. 🙃

To buy myself some time, I decided to redirect the whole website to my existing website , where I could temporarily host the content for the challenge.

Initially, I tried to create a solution that would allow me to create multiple redirects, but it didn't work out. Instead, I created a quick proof-of-concept using Traefik , a tool I often use in my infrastructure. 😎

Solution

Here is the final solution I came up with using Traefik:

version: "3.8"
services:
  merisia:
    # As we are using Docker Swarm, labels need to be set inside deploy
    deploy:
      labels:
        traefik.docker.network: "traefik-net"
        traefik.enable: "true"
        traefik.http.routers.merisia.entrypoints: "https"
        traefik.http.routers.merisia.middlewares: "redirect-merisia-benjaminrancourt,default@file"
        traefik.http.routers.merisia.rule: "Host(`merisia.ca`,`www.merisia.ca`)"
        traefik.http.routers.merisia.tls.certresolver: "letsEncrypt"
        traefik.http.routers.merisia.tls.options: "intermediate@file"
        traefik.http.routers.merisia.tls: "true"
        traefik.http.services.merisia.loadbalancer.server.port: 80
        traefik.http.services.merisia.loadbalancer.sticky.cookie.httpOnly: "true"
        traefik.http.services.merisia.loadbalancer.sticky.cookie.secure: "true"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.regex: "^https?://(www\\.)?merisia\\.ca/(.*)"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.replacement: "https://www.benjaminrancourt.ca/$${2}"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.permanent: "false"
      resources:
        limits:
          cpus: '0.750'
          memory: 16M
        reservations:
          cpus: '0.001'
          memory: 6M
    # https://hub.docker.com/r/traefik/whoami/tags?page=1&ordering=last_updated
    image: "traefik/whoami:v1.8.7"
    networks:
      - traefik-net
networks: