Redirecting to an external domain with traefik
R edirecting to another domain is a regular use case I need a lot. Luckily Traefik makes it easy to implement it.
In this article I will show you how to use redirects to an external domain in Traefik. The example can be executed locally which allows simple adjustment to your own needs. Technologies used are only docker and docker-compose. For the purpose of simpler declaration I will not make use of configuration files, but only use docker labels.
Traefik configuration
A minimalistic configuration of Traefik can be seen in the code block below.
Only port 80
is exposed end assigned to the web
entrypoint.
All incoming traffic is now routed through this specific entrypoint.
The configuration of the redirect is in the labels section of the Traefik container. No further containers are needed.
In the labels section a router listening to the URL redirect.localhost
in the web entrypoint is defined.
A middleware of the type redirectregex
is dealing with all incoming requests.
A regex is defined to filter the incoming requests.
The regex (.)*
will allow all incoming requests.
None will be filtered out.
The replacement is the URL where we want to redirect to.
In this case it is set to my website.
For testing purposes I would recommend to set the redirect to temporarily (HTTP Code 302) by setting the attribute permanent
to false
.
This avoids a lot of pain while testing, because the browser will not cache the redirect.
Depending on your use case you might want to set it to permanent
(HTTP Code 301) as soon as you are done.
You should be aware that multiple levels of redirects do not work.
If you also want to do a global https and www redirect for your domain you are not able to do both. For that I would recommend using the following docker container and keep the global redirect.
Make sure to add https://
or only the curent path will be replaced and you would end up at redirect.localhost/jensknipper.de
.
If you now start the service with docker-compose up
and navigate to redirect.localhost
you should be redirected to my website.
Further readings
As always the full source code is available on GitHub.