Traefik http/https redirect www to non-www

Setting up Traefik to receive requests from both www and non-www, and redirect all of the www requests to non-www (for better SEO).

Marco Martino
Soulweb Academy
2 min readNov 11, 2022

--

Photo by Mark König on Unsplash

What are we using

Requirements

Our mission

In order to make this work we are going to:

  1. Setup a basic Traefik container
  2. Setup the dynamic configuration on Traefik where to place the configuration needed for the redirect
  3. Setup the redirect middlewares (http + https) for Traefik
  4. Setup a Whoami host service to test the redirect

Setup Traefik

In a brand new directory, let’s create the traefik.yml file:

#traefik.yml
version: '3.9'

services:
traefik:
image: traefik:v2.9
container_name: traefik
command:
- --providers.docker=true
# Define Traefik entry points to port [80] for http and port [443] for https.
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
restart: always
networks:
# Define the network on which traefik is going to operate.
- web
ports:
# Open traefik http [80] and https [443] ports.
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- "traefik.enable=true"

networks:
# Define the network on which is going to operate Traefik.
web:
external: true

Setup Traefik dynamic conf

Now, let’s create the directory traefik.d and add it to our Traefik service.

#traefik.yml
# Configure the Traefik dynamic conf directory
command:
...
- --providers.file.directory=/traefik/traefik.d

# Mount the dynamic conf directory on the Traefik host
volumes:
...
- ./traefik.d:/traefik/traefik.d

Setup Traefik middlewares

It’s time to setup our redirection!

To do that, we need to setup a couple of Traefik middlewares for both http and https. So, let’s create the file middlewares.yml within the folder traefik.d:

# middlewares.yml
http:
middlewares:
redirect-http-www:
redirectRegex:
regex: "^http?://www\\.(.+)"
replacement: "http://${1}"
permanent: true
redirect-https-www:
redirectRegex:
regex: "^https?://www\\.(.+)"
replacement: "https://${1}"
permanent: true

Setup test host

We’ll use a Whoami host to test the redirection. Let’s add the whoami service to our traefik.yml

#traefik.yml
...

whoami:
image: traefik/whoami:latest
container_name: whoami
networks:
- web
labels:
# Define traefik network to use
- "traefik.docker.network=web"
# Enable https
- "traefik.http.routers.whoami.tls=true"
# Define www and non-www host
- "traefik.http.routers.whoami.rule=Host(`mywhoami.com`) || Host(`www.mywhoami.com`)"
# Add redirect middlewares for http and https
- "traefik.http.routers.whoami.middlewares=redirect-http-www@file,redirect-https-www@file"

...

Finally, we just have to replace the Host domains with the real one and we are done!

#traefik.yml
# Replace the Host domains on this row
- "traefik.http.routers.whoami.rule=Host(`mywhoami.com`) || Host(`www.mywhoami.com`)"

Run and test it!

To run:

docker compose network create web
docker compose -f traefik.yml up -d

# or, for oldest docker-compose
docker-compose network create web
docker-compose -f traefik.yml up -d

Once up, open your favorite browser and try to navigate to the www domain you setup just above for the whoami service, in my case they are:

If everything works as aspected, you’ll be redirect to the non-www domain!

You can find the whole configuration on this repository.

--

--

Marco Martino
Soulweb Academy

Software Engineer, Open Source enthusiast, Full-stack Developer, Drupal specialist, React Developer, GIS Developer, Data lover