# Problem with www to non-www redirect because of HSTS

**URL:** <https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580>\
**Category:** Traefik v2\
**Tags:** middleware\
**Created:** [June 19, 2020, 2:51pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580 "2020-06-19T14:51:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![felixsanz](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/felixsanz/32/2216_2.png) [@felixsanz](https://community.traefik.io/u/felixsanz)\
**Post date:** [June 19, 2020, 2:51pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580/1 "2020-06-19T14:51:00Z")

</div>

I'm trying to redirect `https://www.domain.com` to `https://domain.com` but I have an issue. When I curl `https://www.domain.com` , I get a HSTS error and i'm not redirected. Basically my browser (curl) wants to approve the SSL certificate first I think, and it's failing, that's why chrome shows an HSTS issue.

This is my router, i tried removing the `tls:` option too

```auto
    www-to-nonwww:
      entryPoints:
        - https
      rule: "HostRegexp(`{host:(www\\.).+}`)"
      service: noop@internal
      tls:
        certResolver: le
      middlewares:
        - https-nonwww

```

My `certResolver` works fine with the normal router (the one with `rule: https://domain.com`)

Any idea?

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [June 19, 2020, 3:38pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580/2 "2020-06-19T15:38:23Z")

</div>

I'm sure that curl does not care for HSTS, it will be a certificate error because it will be using the self-signed traefik default certificate.

Chrome will show the HSTS error for the same reason. It is an HSTS error (vs a plain certificate error) because this site has presented an HSTS at some point in the past or it is an HSTS site compiled into the browser.

You have no definitive host in the rule for le to resolve a certificate to.

You can either make your rule a Host rule `Host(`www.domain.com`)`

Or add more tls options to your router:

```auto
      tls:
        certResolver: le
        domains:
          - main: "domain.com"
             sans: 
             - "www.domain.com"

```

---

<div class="post-metadata">

**Author:** ![felixsanz](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/felixsanz/32/2216_2.png) [@felixsanz](https://community.traefik.io/u/felixsanz)\
**Post date:** [June 19, 2020, 4:39pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580/3 "2020-06-19T16:39:16Z")

</div>

yea, curl throws an error, chrome shows HSTS issue.

about your solution... my approach serves to redirect ALL websites, not just a single one, so... how i can make it not dependent on main: [domain.com](http://domain.com)?

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [June 19, 2020, 8:04pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580/4 "2020-06-19T20:04:42Z")

</div>

Sorry hopefully I did not conflate your issue. You could do it like this for multiple certificates for www. redirects:

```auto
      tls:
        certResolver: le
        domains:
          - main: "www.domain1.com"
             sans: 
             - "www.domain2.com"
             - "www.someplace.io"
             - "www.example.net"

```

Personally I would set the www and the bare domain on the router handling the domain. Or declare multiple in one route somewhere.

---

<div class="post-metadata">

**Author:** ![felixsanz](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/felixsanz/32/2216_2.png) [@felixsanz](https://community.traefik.io/u/felixsanz)\
**Post date:** [June 20, 2020, 7:48pm UTC](https://community.traefik.io/t/problem-with-www-to-non-www-redirect-because-of-hsts/6580/5 "2020-06-20T19:48:47Z")

</div>

thanks! that helps 🙂
