Redirect www to non-www not working in V2

Ive tried everything in the forums to redirect www to non www but it doesnt work

labels:
      - "traefik.enable=true"
      - "traefik.http.routers.node.entrypoints=http"
      - "traefik.http.routers.node.rule=Host(`mydomain.com`) || Host(`www.mydomain.com`)"
      - "traefik.http.middlewares.vk-https-redirect.redirectscheme.scheme=https"

     # - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://www.mydomain.com/(.*)"
     # - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://mydomain.com/$${1}"
     
	  - "traefik.http.middlewares.redirect-to-https.redirectregex.regex=^https?://(?:www\\.)?(.+)"
      - "traefik.http.middlewares.redirect-to-https.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.redirect-to-https.redirectregex.permanent=true"

      - "traefik.http.routers.node.middlewares=vk-https-redirect"
      - "traefik.http.routers.node-secure.entrypoints=https"
      - "traefik.http.routers.node-secure.rule=Host(`mydomain.com`) || Host(`www.mydomain.com`)"
      - "traefik.http.routers.node-secure.tls=true"
      - "traefik.http.routers.node-secure.tls.certresolver=http"
      - "traefik.http.routers.node-secure.service=node"
      - "traefik.http.services.node.loadbalancer.server.port=8000"
      - "traefik.docker.network=proxy"

Help Please

Hi,

Similar issue. Redirect non-www to www not working.

Method 1 :

- "traefik.http.routers.foo.rule=Host(`www.foo.fr`) || (Host(`foo.fr`))"
- "traefik.http.middlewares.foo.redirectregex.regex=^http?://foo.fr/(.*)"
- "traefik.http.middlewares.foo.redirectregex.replacement=http://www.foo.fr/$${1}"

Method 2 :

- "traefik.http.routers.foo.rule=Host(`www.foo.fr`) || (Host(`foo.fr`))"
- "traefik.http.routers.foo.middlewares=foo-redirectregex@file"

In config.yml file :

    foo-redirectregex:
      redirectRegex:
        regex: "^https?://foo.fr/(.*)"
        replacement: "http://www.foo.fr/$1"
        permanent: true

Doc : https://docs.traefik.io/middlewares/redirectregex/

Regex : https://github.com/containous/traefik/issues/572#issuecomment-382270057
This Regex work on regex101 website.

I used this before migrating to v2 :

      - "traefik.frontend.rule=Host:foo.fr,www.foo.fr"
      - "traefik.frontend.redirect.regex=^https?://foo.fr/(.*)"
      - "traefik.frontend.redirect.replacement=https://www.foo.fr/$${1}"

Any idea ?

Regards
Pausecafe

Take a look to Global redirect www to non-www with HTTPS redirection

Thanks you Idez but I have already read your post. I use same label as you.

I want use a specific redirect only : http(s)://foo.fr --> http(s)://www.foo.fr .

I don't understand the issue. it seems right for me.

@pausecafe The issue is you havent applied the middleware to your https route. I had the same problem

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.node.entrypoints=http"
  - "traefik.http.routers.node.rule=Host(`awebsite.com`) || Host(`www.awebsite.com`)"
  - "traefik.http.middlewares.vk-https-redirect.redirectscheme.scheme=https"

  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://www.awebsite.com/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://awebsite.com/$${1}"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.permanent=true"
  - "traefik.http.routers.node.middlewares=vk-https-redirect"
  - "traefik.http.routers.node-secure.entrypoints=https"
  - "traefik.http.routers.node-secure.rule=Host(`awebsite.com`) || Host(`www.awebsite.com`)"
  - "traefik.http.routers.node-secure.tls=true"
  - "traefik.http.routers.node-secure.tls.certresolver=http"
  - "traefik.http.routers.node-secure.service=node"
  - "traefik.http.services.node.loadbalancer.server.port=8000"
  - "traefik.docker.network=proxy"

I had to add

- "traefik.http.routers.node-secure.middlewares=test-redirectregex"

@webface Thanks you. It works :white_check_mark:

Resume :

- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.services.foo.loadbalancer.server.port=80"

- "traefik.http.routers.foo.entrypoints=http"
- "traefik.http.routers.foo.rule=Host(`foo.fr`) || (Host(`www.foo.fr`))"
- "traefik.http.routers.foo.middlewares=https-redirect@file,foo-redirectregex"

- "traefik.http.routers.foo-secure.entrypoints=https"
- "traefik.http.routers.foo-secure.rule=Host(`foo.fr`) || (Host(`www.foo.fr`))"
- "traefik.http.routers.foo-secure.middlewares=https-redirect@file,foo-redirectregex"
- "traefik.http.routers.foo-secure.tls=true"
- "traefik.http.routers.foo-secure.tls.certresolver=http"
- "traefik.http.routers.foo-secure.service=foo"

- "traefik.http.middlewares.foo-redirectregex.redirectregex.regex=^https?://foo.fr/(.*)"
- "traefik.http.middlewares.foo-redirectregex.redirectregex.replacement=https://www.foo.fr/$${1}"
- "traefik.http.middlewares.foo-redirectregex.redirectregex.permanent=true"

Result :

  • middleware https-redirect@file
# curl -I http://foo.fr
Location: https://foo.fr/
  • middleware foo-redirectregex
# curl -I https://foo.fr
Location: https://www.foo.fr/