Cannot redirect from non-www to www with traefik 2

Hello I'm trying to redirect every non www url to a www url

Following the guide redacted by the moderator kakiwi there => Redirect from non-www to www with traefik 2

My config was running as expected just using host without www but we decided to redirect everything on www. url ...

I've two docker-compose.yml in /aFolder/traefik/docker-compose.yml
And the second in /aFolder/myApp/docker-compose.yml

The traefik/docker-compose.yml :

...
labels:
      ...
      # low priority rule for otherwise unmatched www. host prefixes
      # without this it is constant redirect
      - "traefik.http.routers.unmatchedwww.rule=HostRegexp(`{name:^www\\..*}`)"
      - "traefik.http.routers.unmatchedwww.service=noop@internal"
      - "traefik.http.routers.unmatchedwww.priority=2"
      # lowest priority catchall rule, will add/replace www. host portion
      - "traefik.http.routers.matchlast.rule=PathPrefix(`/`)"
      - "traefik.http.routers.matchlast.priority=1"
      - "traefik.http.routers.matchlast.middlewares=addwww"
      - "traefik.http.middlewares.addwww.redirectregex.regex=^https://(?:www\\.)?(.*)"
      - "traefik.http.middlewares.addwww.redirectregex.replacement=https://www.$$1"

myApp/docker-compose.yml :

version: "3.8"
services:

  my-app:
    container_name: my-app
    image: my-app
    build:
      context: .
      dockerfile: Dockerfile
    networks:
      - traefik-network
    ports:
      - ":80"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.my-app.rule=Host(`www.my-app.com`)"
      - "traefik.http.routers.my-app.tls.domains[0].main=my-app.com"
      - "traefik.http.routers.my-app.tls.domains[0].sans=www.my-app.com"
      - "traefik.http.routers.my-app.service=my-app"
      - "traefik.http.routers.my-app.entrypoints=websecure"
      - "traefik.http.services.my-app.loadbalancer.server.port=80"
      - "traefik.http.routers.my-app.tls=true"
      - "traefik.http.routers.my-app.tls.certresolver=myresolver"
      - "traefik.http.services.my-app.loadbalancer.passhostheader=true"
      - "traefik.docker.network=traefik-network"
    restart: unless-stopped

networks:
  traefik-network:
    external: true

Traefik dashboard available,
www.my-app.com available
my-app.com => 404 page not found

docker logs traefik :

IP - - [14/Dec/2023:15:29:50 +0000] "GET / HTTP/2.0" 404 19 "-" "-" 912 "-" "-" 0ms
IP - - [14/Dec/2023:15:29:50 +0000] "GET /favicon.ico HTTP/2.0" 404 19 "-" "-" 913 "-" "-" 0m

I've been trying to make this work for two days now. Any help would be greatly appreciated :pray:

Thanks in advance

Regex for Traefik can be special :sweat_smile:

From www to none is easy:

  whoami:
    image: traefik/whoami:v1.8
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywhoami.rule=Host(`whoami.example.com`) || Host(`www.whoami.example.com`)
      - traefik.http.services.mywhoami.loadbalancer.server.port=80

      - traefik.http.middlewares.mywwwredirect.redirectregex.regex=^https://www\.(.*)
      - traefik.http.middlewares.mywwwredirect.redirectregex.replacement=https://$${1}
      - traefik.http.routers.mywhoami.middlewares=mywwwredirect

But maybe you are just missing the right rule:

rule=Host(`www.my-app.com`) || Host(`my-app.com`)

Thanks for your time, that was due to missconfig and missing www. on app domain in .env config files
The docker-compose was OK ^^ :sweat:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.