V2.3.5 www to non-www redirect

I have failed to remove www. How can I achieve it?

This is traefik v2.3.5 docker-compose.yml file.

version: "3.7"

services:
  traefik:
    image: traefik:v2.3.5
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.yml:/traefik.yml:ro
      - ./acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      # global redirection: https (www.) to https
      - "traefik.http.routers.wwwsecure-catchall.rule=HostRegexp(`{host:(www\\.).+}`)"
      - "traefik.http.routers.wwwsecure-catchall.entrypoints=websecure"
      - "traefik.http.routers.wwwsecure-catchall.tls=true"
      - "traefik.http.routers.wwwsecure-catchall.middlewares=wwwtohttps"
      # middleware: http(s)://(www.) to  https://
      - "traefik.http.middlewares.wwwtohttps.redirectregex.regex=^https?://(?:www\\.)?(.+)"
      - "traefik.http.middlewares.wwwtohttps.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.wwwtohttps.redirectregex.permanent=true"
      # v2.3.5 config
      - "traefik.http.routers.traefik.rule=Host(`traefik.domain.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=password"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.domain.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"
      - "traefik.http.services.traefik.loadbalancer.server.port=8080"
      # gzip compression
      - "traefik.http.routers.traefik.middlewares=traefik-compress"
      - "traefik.http.middlewares.traefik-compress.compress=true"

networks:
  proxy:
    external: true

This is container's docker-compose.yml file.

version: '3.7'

services:
    bodrum-web:
      image: docker-image-url
      container_name: bodrum-web
      restart: unless-stopped
      security_opt:
        - no-new-privileges:true
      networks:
        - proxy
      volumes:
        - /etc/localtime:/etc/localtime:ro
        - /var/run/docker.sock:/var/run/docker.sock:ro
        - ./data:/data
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.bodrum-web.entrypoints=http"
        - "traefik.http.routers.bodrum-web.rule=Host(`domain.com`) || Host(`www.domain.com`)"
        - "traefik.http.middlewares.bodrum-web-https-redirect.redirectscheme.scheme=https"
        - "traefik.http.routers.bodrum-web.middlewares=bodrum-web-https-redirect"
        - "traefik.http.routers.bodrum-web-secure.entrypoints=https"
        - "traefik.http.routers.bodrum-web-secure.rule=Host(`domain.com`) || Host(`www.domain.com`)"
        - "traefik.http.routers.bodrum-web-secure.tls=true"
        - "traefik.http.routers.bodrum-web-secure.tls.certresolver=http"
        - "traefik.http.routers.bodrum-web-secure.service=bodrum-web"
        - "traefik.http.services.bodrum-web.loadbalancer.server.port=80"
        - "traefik.docker.network=proxy"
        # gzip compression
        - "traefik.http.middlewares.bodrum-compress.compress=true"
        - "traefik.http.routers.bodrum-web.middlewares=bodrum-compress"

networks:
  proxy:
    external: true

Hello @fatihyildizhan,

I tried to find your issue but it was a bit hard without your static configuration, so I have made some assumptions. In the Traefik configuration, I found the following errors:

  • I might be wrong but I the entrypoints attached to wwwsecure-catchall router must be https.
  • Because of the router priorities, if you want this router to catch all www requests, then you will have to assign it a high priority (something like traefik.http.routers.wwwsecure-catchall.priority=10000).

In the container configuration:

  • In your bodrum-web-secure router, I think the Host(www.domain.com) rule is not needed
  • When you attach the bodrum-compress middlewares to the bodrum-web router you are overriding the redirect middleware you have already defined. To attach both middlewares, you should do: traefik.http.routers.bodrum-web.middlewares=bodrum-web-https-redirect,bodrum-compress

As a side note, I think it's possible to simplify your configuration (removing the http router in your container config) by using entrypoint redirection.

Hope this helps!