# Global http -\> https and www -\> non-www

**URL:** <https://community.traefik.io/t/global-http-https-and-www-non-www/22574>\
**Category:** Traefik v3 (latest)\
**Tags:** middleware, docker\
**Created:** [May 4, 2024, 12:32pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574 "2024-05-04T12:32:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![predmijat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/predmijat/32/2631_2.png) [@predmijat](https://community.traefik.io/u/predmijat)\
**Post date:** [May 4, 2024, 12:32pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/1 "2024-05-04T12:32:46Z")

</div>

Continuing the discussion from [Global redirect www to non-www with HTTPS redirection](https://community.traefik.io/t/global-redirect-www-to-non-www-with-https-redirection/2313/9):

Hey, I tried the solution from this topic in Traefik v3.0, but I can't get it to work in all cases (and from what I see, that issue is reported even in that older topic).

I'm trying to configure all this in docker-compose.yml (docker labels).

http://$domain redirects to https://$domain  
[http://www.$domain](http://www.$domain) redirects to [https://www.$domain](https://www.$domain)  
but  
[https://www.$domain](https://www.$domain) gives certificate error (default Traefik certificate is served).

I'd like that [https://www.$domain](https://www.$domain) is also redirected to https://$domain.

So no matter what client types I'd like it to end up on https://$domain. No http, no www.

Thanks!

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [May 4, 2024, 5:15pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/2 "2024-05-04T17:15:13Z")

</div>

See [simple Traefik example](https://github.com/bluepuma77/traefik-best-practice/tree/main/docker-traefik-dashboard-letsencrypt). Make sure to use

```auto
.rule=Host(`domain.tld`) || Host(`www.domain.tld`)

```

So domain and sub-domain can be recognized and both LE certs can be created.

---

<div class="post-metadata">

**Author:** ![predmijat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/predmijat/32/2631_2.png) [@predmijat](https://community.traefik.io/u/predmijat)\
**Post date:** [May 4, 2024, 6:13pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/3 "2024-05-04T18:13:21Z")

</div>

That's not global though? I want to set it up only in one place.

So something like this, but not broken 🙂

```auto
      - "traefik.http.routers.http-catchall.rule=hostregexp(`(www\\.)?.+`)"
      - "traefik.http.routers.http-catchall.entrypoints=http"
      - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"

      - "traefik.http.routers.www-https-catchall.rule=hostregexp(`(www\\.).+`)"
      - "traefik.http.routers.www-https-catchall.entrypoints=https"
      - "traefik.http.routers.www-https-catchall.tls=true"
      - "traefik.http.routers.www-https-catchall.middlewares=redirect-to-https"

      - "traefik.http.middlewares.redirect-to-https.redirectregex.regex=^https?://(?:www\\.)?(.+)"
      - "traefik.http.middlewares.redirect-to-https.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.redirect-to-https.redirectregex.permanent=true"

```

I'm also confused about the `{1}` (taken from previous thread) - isn't it taken the first capture group while we have two (and need the second)?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [May 4, 2024, 8:02pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/4 "2024-05-04T20:02:25Z")

</div>

Global means you set it up once, valid for all.

Did you check my example?

```auto
      - traefik.http.middlewares.mywwwredirect.redirectregex.regex=^https://www\.(.*)
      - traefik.http.middlewares.mywwwredirect.redirectregex.replacement=https://$${1}
      - traefik.http.routers.mywhoami.middlewares=mywwwredirect

```

---

<div class="post-metadata">

**Author:** ![predmijat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/predmijat/32/2631_2.png) [@predmijat](https://community.traefik.io/u/predmijat)\
**Post date:** [May 4, 2024, 9:00pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/5 "2024-05-04T21:00:01Z")

</div>

I don't think we understand each other.  
What I'm trying to achieve (those 3 points from the original post) should all be defined in docker-compose.yml which should just spin up Traefik. I have other docker-compose.ymls for different services/websites, but I want all redirects to happen in Traefik's definition.

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [May 5, 2024, 7:06am UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/6 "2024-05-05T07:06:13Z")

</div>

You can create a http-to-https redirect directly on `entrypoint` ([doc](https://doc.traefik.io/traefik/routing/entrypoints/#redirection)).

You can assign a www redirect middleware directly on `entrypoint` ([doc](https://doc.traefik.io/traefik/routing/entrypoints/#middlewares)), but you need to use `providers.file` to read it from an extra dynamic config file.

---

<div class="post-metadata">

**Author:** ![predmijat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/predmijat/32/2631_2.png) [@predmijat](https://community.traefik.io/u/predmijat)\
**Post date:** [May 5, 2024, 4:28pm UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/7 "2024-05-05T16:28:48Z")

</div>

Ok, thank you.

Anyone else with a more elegant solution maybe?

---

<div class="post-metadata">

**Author:** ![TBG-FR](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tbg-fr/32/8356_2.png) [@TBG-FR](https://community.traefik.io/u/TBG-FR)\
**Post date:** [August 19, 2024, 7:09am UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/8 "2024-08-19T07:09:44Z")

</div>

I guess `--entryPoints.websecure.http.middlewares=mymiddleware@docker` should work too, not only `--entryPoints.websecure.http.middlewares=mymiddleware@file`, no ?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [August 19, 2024, 9:10am UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/9 "2024-08-19T09:10:32Z")

</div>

It should work. Personally I am hesitant to use a dynamic middleware assignment in static config, as there will always be a delay for loading the middleware from Docker configuration.

---

<div class="post-metadata">

**Author:** ![TBG-FR](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tbg-fr/32/8356_2.png) [@TBG-FR](https://community.traefik.io/u/TBG-FR)\
**Post date:** [August 20, 2024, 9:04am UTC](https://community.traefik.io/t/global-http-https-and-www-non-www/22574/10 "2024-08-20T09:04:15Z")

</div>

I just tried it and it works well (`--entrypoints.websecure.http.middlewares=wwwtohttps@docker`)

I didn't notice any difference or delay, but I understand your concern
