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:
traefik-net:
driver: overlay
external: true
Let me explain how this solution works below. 😉
Explanations
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(`merisia.ca`,`www.merisia.ca`)"
This rule tells Traefik the original domain to redirect from. In this case, the domain is merisia.ca 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: "https://www.benjaminrancourt.ca/$${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"
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! 😉
Conclusion
In my case, the previous solution has helped me to have www.merisia.ca/linkedin temporarily redirect to www.benjaminrancourt.ca/linkedin, 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! 😎