Remove www in URL using Traefik

2022-06-24

As you might have noticed, I have been playing around with Traefik (opens new window) recently. One of the things I have missed to do so far is to remove the www. from URLs of my sites. Today we are going to fix that! 🤓

If you are new to Traefik, you might want to check out the Traefik documentation (opens new window) or one of my other articles on how to use Traefik.

Ok, so with that out of the way, let's get started! 💪

The first thing we need to know is the difference between static and dynamic configuration in Traefik.

# Static Configuration

The static configuration (opens new window) refers to the startup configuration of Traefik. Elements in the static configuration set up the providers and entrypoints Traefik will listen to.

I usually make use of the CLI configuration (opens new window) to setup Traefik (opens new window), but you can also use a file configuration (opens new window) instead.

Check out the provided links, to see examples.

# Dynamic Configuration

In order to setup the dynamic configuration, we need to provide a directory that holds all the different configuration files. The path to the file needs to point to a location inside your docker container.

services:
  traefik:
    image: traefik:2.6
    restart: ${RESTART}
    command:
      # Setup dynamic configuration
      - "--providers.file.directory=/etc/traefik/traefik.d"
    volumes:
      # Mount the dynamic configuration directory to the container
      - ./traefik.d:/etc/traefik/traefik.d

Here is a full example (opens new window) from GitHub.

Inside the traefik.d directory you can now place all sort of configuration files. See the documentation (opens new window) for more information on that.

# Removing the www. from URLs

In order to remove the www. from URLs, we need to create a middleware that can handle this task. Middlewares are part of the dynamic configuration. That's why we are going to create the file traefik.d/middlewares.yml with the following content inside of traefik.d.

http:
  middlewares:
    redirect-to-non-www:
      redirectRegex:
        regex: "^https://www\\.(.*)"
        replacement: "https://${1}"
        permanent: true

In this example we are using the RedirectRegex (opens new window) middleware. This middleware allows us to search for a regular expression and replace it with a different string, that will be used to redirect the user to the URL without the www..

To apply the middleware, we on a docker-compose setup, we can add the following to the labels of the given container.

    labels:
      # ...
      # middlewares
      - "traefik.http.routers.your-container-name.middlewares=redirect-to-non-www@file"

If you are applying middlewares to your container already, you can just add more middlewares, by separating them with a comma.

      # middlewares
      - "traefik.http.routers.your-container-name.middlewares=one,two,three,redirect-to-non-www@file"

And that's basically it! 😁

# Full Example

If you want to see a full example, you can check out my tjventurini/traefik-reverse-proxy (opens new window) repository. The repository contains a simple but useful Traefik setup, containing the code from this example.