redirectRegex not working / redirecting onto the www subdomain

I'm trying to redirect all traffic from HTTP to HTTPS, and from the root domain to the www subdomain. I've solved the former using some help here, and am trying the same approach for the www subdomain. Here's my full dynamic configuration:

http:
  routers:
    https-redirect:
      rule: "HostRegexp(`{any:.*}`)"
      middlewares: ["https-redirect"]
      entryPoints: ["web"]
      service: "dummy"

    www-redirect:
      rule: "Host(`transiter.io`)"
      middlewares: ["www-redirect"]
      entryPoints: ["websecure"]
      service: "dummy"


  middlewares:
    https-redirect:
      redirectScheme:
        scheme: "https"

    www-redirect:
      redirectRegex:
        regex: "^https://transiter.io/(.*)"
        replacement: "https://www.transiter.io/${1}"
        permanent: true

  services:
    dummy:
      loadBalancer:
        servers:
        - url: "https://www.google.com"

However, when I launch this and hit https://transiter.io, I get a 404 error instead of being redirected onto the www subdomain. Any ideas why this isn't working? I can't for the life of me see why the www-redirect middleware is simply being ignored.

I've seen "workarounds" where people add to every individual service a redirect onto the www subdomain. I think this is kind of crazy because such redirections shouldn't be the responsibility of individual services at all, but rather the layer in front of them.

Just to say, solving this kind of problem with an Nginx reverse proxy is trivial. The idea of Traefik is really cool, but solving really basic problems like this seems so, so hard and is a real barrier to using it :frowning:

1 Like

Hello,

As TLS configuration is on the router and not on the entry point, you have to setup on the router.

http:
  routers:
    https-redirect:
      rule: "HostRegexp(`{any:.*}`)"
      middlewares: ["https-redirect"]
      entryPoints: ["web"]
      service: "dummy"

    wwwredirect:
      rule: "Host(`transiter.io`, `www.transiter.io`)"
      middlewares: ["wwwredirect"]
      entryPoints: ["websecure"]
      service: "dummy"
      tls: {}

  middlewares:
    https-redirect:
      redirectScheme:
        scheme: "https"

    wwwredirect:
      redirectRegex:
        regex: "^https://transiter\\.io/(.*)"
        replacement: "https://www.transiter.io/${1}"
        permanent: true

  services:
    dummy:
      loadBalancer:
        servers:
          - url: "https://www.google.com"

Awesome, it works, thank you so much. :slight_smile: