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. 😎


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

version: "3.8"

    # As we are using Docker Swarm, labels need to be set inside deploy
      labels: "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(``,``)"
        traefik.http.routers.merisia.tls.certresolver: "letsEncrypt"
        traefik.http.routers.merisia.tls.options: "intermediate@file"
        traefik.http.routers.merisia.tls: "true" 80 "true" "true"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.regex: "^https?://(www\\.)?merisia\\.ca/(.*)"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.replacement: "$${2}"
        traefik.http.middlewares.redirect-merisia-benjaminrancourt.redirectregex.permanent: "false"
          cpus: '0.750'
          memory: 16M
          cpus: '0.001'
          memory: 6M
    image: "traefik/whoami:v1.8.7"
      - traefik-net

    driver: overlay
    external: true

Let me explain how this solution works below. 😉


Note: to improve the readability of the code, I removed the indentation and renamed the middleware as redirection. Because of that, the excerpts below would not work with the solution above without modification.

1 – Router rule

traefik.http.routers.merisia.rule: "Host(``,``)"

This rule tells Traefik the original domain to redirect from. In this case, the domain is and its subdomain is www.

2 – Redirect regex middleware

 traefik.http.middlewares.redirection.redirectregex.regex: "^https?://(www\\.)?merisia\\.ca/(.*)"
 traefik.http.middlewares.redirection.redirectregex.replacement: "$${2}"
 traefik.http.middlewares.redirection.redirectregex.permanent: "false"

The regex redirects the original traffic to the replacement domain. It captures the original traffic using a regular expression (in regex) and redirects it to the replacement domain using a replacement pattern (in replacement).

The regex is designed to work with both HTTP and HTTPS (^https?://) and to capture all traffic that come from its www subdomain and its bare domain ((www\\.)?). All dots were also escaped accordingly (\\.).

The pathname is captured in the second and last capture group ($${2}).

3 – Middlewares

This label tells Traefik to use the previous middleware.

traefik.http.routers.merisia.middlewares: "redirection,default@file"
Two middlewares is used here: our redirect middleware and the unknown default from the configuration file.

4 – Image

And finally, we also need to have a container that simply exists, as I don't think it's possible to have a docker-compose stack without a container! 😅

image: "traefik/whoami:v1.8.7"

The traefik/whoami image is a tiny web server that prints OS information and HTTP request to output. But, if you have configured everything correctly, you should never see its output, as you should be redirected to your second domain! 😉


In my case, the previous solution has helped me to have temporarily redirect to, which finally redirect to my personal LinkedIn profile. I also had some others links, but I'm not able to disclosure yet. 😅

So, if you use Traefik, you now have a solution to temporarily redirect traffic from one domain to another. 😉

Good luck! 😎