# redirectRegex not working / redirecting onto the www subdomain

**URL:** <https://community.traefik.io/t/redirectregex-not-working-redirecting-onto-the-www-subdomain/1792>\
**Category:** Traefik v2\
**Tags:** file\
**Created:** [September 22, 2019, 6:47pm UTC](https://community.traefik.io/t/redirectregex-not-working-redirecting-onto-the-www-subdomain/1792 "2019-09-22T18:47:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamespfennell](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jamespfennell](https://community.traefik.io/u/jamespfennell)\
**Post date:** [September 22, 2019, 6:47pm UTC](https://community.traefik.io/t/redirectregex-not-working-redirecting-onto-the-www-subdomain/1792/1 "2019-09-22T18:47:15Z")

</div>

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:

```auto
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 ☹

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [September 22, 2019, 9:59pm UTC](https://community.traefik.io/t/redirectregex-not-working-redirecting-onto-the-www-subdomain/1792/2 "2019-09-22T21:59:10Z")

</div>

Hello,

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

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![jamespfennell](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jamespfennell](https://community.traefik.io/u/jamespfennell)\
**Post date:** [September 23, 2019, 12:48am UTC](https://community.traefik.io/t/redirectregex-not-working-redirecting-onto-the-www-subdomain/1792/3 "2019-09-23T00:48:34Z")

</div>

Awesome, it works, thank you so much. 🙂
