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.

Hello JaneJeon, if you still care about this issue ive made a successfull version of this in my mug project
where it redirects www.mug.micheleawada.com to mug.micheleawada.com
both in https and http

however in my version ive made it so web secure does not contain the middleware instead each traefik container contains chooses the middleware according to what it wants

Hello Traefik Community,

I wanted to share a solution we've implemented for a common requirement: redirecting www URLs to non-www using Traefik in a Kubernetes environment. This might be helpful if you're looking to streamline your URL structure for SEO or consistency.

Initial Challenge:

We needed to create a middleware in Traefik that would redirect all traffic from www.example.com to example.com. The key was to ensure this redirection was handled efficiently and consistently across our services.

Solution:

  1. Created a Middleware for Redirects:
  • We defined a middleware resource in Kubernetes to handle the redirect.
  • The middleware uses a regex pattern to match www URLs and redirect them to their non-www counterparts.
  1. Updated IngressRoute Configuration:
  • We then applied this middleware to our existing IngressRoutes.
  • This step was crucial to ensure that the middleware processes the traffic for the specified hosts.
  1. Ensured Regex Covers HTTP and HTTPS:
  • Our initial regex only covered HTTPS traffic. We modified it to handle both HTTP and HTTPS, making it more robust.
  1. Tested Our Configuration:
  • After deploying these changes, we thoroughly tested our setup. It's essential to confirm that the redirects work as expected.

Final Middleware Configuration:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: redirect-www-to-non-www
  namespace: production
spec:
  redirectRegex:
    regex: "^https?://www\\.(.+)"
    replacement: "https://$1"
    permanent: true

Sample IngressRoute Update:

- match: "(Host(`api.example.com`) || Host(`www.api.example.com`)) && PathPrefix(`/path`)"
  kind: Rule
  middlewares:
    - name: redirect-www-to-non-www
  services:
    - name: your-service
      port: your-port

Key Takeaways:

  • Regex Patterns: Ensure your regex correctly matches the URLs you intend to redirect.
  • Middleware Application: Apply the middleware to the appropriate routes in your IngressRoute configurations.
  • Testing: Always test your configuration in a controlled environment before rolling out to production.

This approach has streamlined our URL structure significantly. If you're facing a similar challenge, I hope this solution proves useful. Feedback or further suggestions are always welcome!

Best,