Redirecting www to non-www?

First of all, I'm aware that Global redirect www to non-www with HTTPS redirection exists, but unfortunately I've tried everything here (and searched around the forum for more solutions), to no avail.

What I'm trying to do, is to set up a global www to non-www redirection.

To that end, I've set up a "global" middleware as follows.

Part of the traefik static config that's relevant:

entryPoints:
  web:
    address: :80
    http:
      redirections:
        entryPoint:
          to: web-secure
          scheme: https
          permanent: true
          priority: 10

  web-secure:
    address: :443
    http:
      middlewares:
        - no-www@file

The file-based dynamic config (and I can confirm that the dynamic config is being picked up properly):

http:
  middlewares:
    no-www:
      redirectregex: # I've also tried redirectRegex, the docs are mixed on this!
        regex: ^(?:https://)?(?:www\.)?(.+)
        replacement: https://${1}
        permanent: true

So the way it's supposed to work is that the regex captures the actual "body" of the URL (ignoring http/https, www or non-www) and redirect to the https, non-www version of it. And I even tried it on the go playground as the docs suggested, which works as expected: The Go Playground

However, when I visit both www.mysite.com and https://www.mysite.com, traefik returns 404, even though the mysite.com and https://mysite.com works fine.

Please, can somebody tell me what I'm missing?

I've tried different regexes such as ^https?://(?:www\.)?(.+), ^https?://(?:www\\.)?(.+), and even escaping the /

Hello @JaneJeon and thanks for your interest in Traefik,

I tried to reproduce your issue but without any success.
Here are my static and dynamic configurations:

static.yml

entryPoints:
  web:
    address: :80
    http:
      redirections:
        entryPoint:
          to: web-secure
          scheme: https
          permanent: true

  web-secure:
    address: :443
    http:
      middlewares:
        - no-www@file

api:
  insecure: true

log:
  level: debug

providers:
  file:
    filename: ../dynamic.yml

dynamic.yml

http:
  routers:
    httpbin:
      rule: HostRegexp(`httpbin.localhost`, `{subdomain:www}.httpbin.localhost`) # <-- must match hosts with and without www 
      entryPoints: [ web-secure ] # <-- attach only to the web-secure entrypoint
      service: httpbin
      tls: {}

  middlewares:
    no-www:
      redirectRegex:
        regex: ^https://(?:www\.)?(.+)
        replacement: https://${1}
        permanent: true

  services:
    httpbin:
      loadBalancer:
        servers:
          - url: https://httpbin.org

If you are trying to browse the following URL http://www.httpbin.localhost at the end you will be redirected to https://httpbin.localhost.

Are you sure that your router is only attached to the web-secure endpoint and that the rule matches Hosts with www and without www?

Hope this helps!

1 Like

Yes, I'm using the exact same middleware (down to the regex) and I've retooled all of my services to ONLY listen to web-secure.

I'm thinking this might be an issue with cloudflare/nameserver than traefik itself, but I've yet been able to pinpoint the source of said error.