# Http to https redirection difference in using CLI vs middelwares

**URL:** <https://community.traefik.io/t/http-to-https-redirection-difference-in-using-cli-vs-middelwares/6813>\
**Category:** Traefik v2\
**Tags:** cli, middleware\
**Created:** [July 8, 2020, 8:17am UTC](https://community.traefik.io/t/http-to-https-redirection-difference-in-using-cli-vs-middelwares/6813 "2020-07-08T08:17:40Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![floatingpurr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/floatingpurr/32/1888_2.png) [@floatingpurr](https://community.traefik.io/u/floatingpurr)\
**Post date:** [July 14, 2020, 8:44am UTC](https://community.traefik.io/t/http-to-https-redirection-difference-in-using-cli-vs-middelwares/6813/9 "2020-07-14T08:44:05Z")

</div>

Thanks for your suggestion but I'm not there, yet.

My rationale:

- Redirect http (`webinsecure`) to https (`websecure`) playing with entrypoints
- Add a default middleware, as suggested, to the `websecure` entrypoint
- Use such a middleware to strip www from requests
- Forward just non-www requests

In order to do that, I borrowed the global middleware described in this answer by @ldez ([Global redirect www to non-www with HTTPS redirection](https://community.containo.us/t/global-redirect-www-to-non-www-with-https-redirection/2313/9)). This is the main Traefik container now:

```auto
command:
      # --entrypoints.<name>.address for ports
      # 80 (i.e., name = webinsercure)
      - --entrypoints.webinsecure.address=:80
      # redirection to https
      - --entrypoints.webinsecure.http.redirections.entrypoint.to=websecure
      - --entrypoints.webinsecure.http.redirections.entrypoint.scheme=https
      # 443 (i.e., name = websecure)
      - --entrypoints.websecure.address=:443
      # default middleware
      - --entrypoints.websecure.http.middlewares=wwwtohttps@docker
labels:
      # middleware: http(s)://(www.) to https://
      traefik.http.middlewares.wwwtohttps.redirectregex.regex: ^https?://(?:www\.)?(.+)
      traefik.http.middlewares.wwwtohttps.redirectregex.replacement: https://$${1}
      traefik.http.middlewares.wwwtohttps.redirectregex.permanent: true

```

Everything looks ok:

 ![Schermata 2020-07-14 alle 10.27.10](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/a/af054bd5f82e8fc4c8f5777f964acc5b1931c812.png)

Then I have services like this one

```auto
  my-test-app:
    image: containous/whoami
    networks:
      # This is the network over which Traefik communicates with other containers.
      - proxy
    labels:
      traefik.enable: true
      traefik.http.routers.my-test-app.rule: Host(`foo.com`)
      traefik.http.routers.my-test-app.entrypoints: websecure
      traefik.http.routers.my-test-app.tls.certresolver: leresolver
      traefik.http.routers.my-test-app.tls.domains[0].main: foo.com
      traefik.http.routers.my-test-app.tls.domains[0].sans: '*.foo.com'

```

But I still get 404 for [www.foo.com](http://www.foo.com).

Please, note that, from the theoretical perspective 😃 , I shouldn't set explicitly "[www.foo.com](http://www.foo.com)"-like rules in each router rule, since the global middleware (`wwwtohttps`) should strip www from requests. (Also in [this answer](https://community.containo.us/t/global-redirect-www-to-non-www-with-https-redirection/2313/9), they do the same)

How can I make this scenario work fine?

---

_[View the full topic](https://community.traefik.io/t/http-to-https-redirection-difference-in-using-cli-vs-middelwares/6813)._
