# Cannot get global http to https redirection to work

**URL:** <https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789>\
**Category:** Traefik v2\
**Tags:** docker, letsencrypt-acme, middleware\
**Created:** [June 1, 2023, 2:03pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789 "2023-06-01T14:03:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maurooyhanart](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@maurooyhanart](https://community.traefik.io/u/maurooyhanart)\
**Post date:** [June 1, 2023, 2:03pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789/1 "2023-06-01T14:03:45Z")

</div>

Hi, I'm new to Traefik and I can't configure it for HTTP to HTTPS redirection, and www to no-www redirection.  
I have read almost every doc and forum, and I think I might have a problem with the regexp.

Situation:  
I have a docker-compose file in where I describe (among many) two services: Traefik, and a Nginx that hosts a web page. It is not the only web page under the domain (lets call it domain.com.ar), but every web page works fine, under [https://domain.com.ar](https://domain.com.ar).  
The domain is certified with Letsencrypt.

My issues:  
-Whenever I type [http://domain.com.ar](http://domain.com.ar), it redirects me to a 404 not found.  
-Whenever I type [https://www.domain.com.ar](https://www.domain.com.ar), it redirects me to a 404 not found (same as above).  
So, http as well as www point me to a 404 not found.

My desired solution:  
I would like the URL to be transformed to [https://domain.com.ar/whatever\_path](https://domain.com.ar/whatever_path).  
I would like to set things with middlewares, since that is the approach that gave me the closest to a solution. But any help is appreciated.

This is the docker compose file.

```auto
services:
  traefik:
    image: "traefik:v2.10"
    command:
      - "--log.level=TRACE"
      - "--api.dashboard=true"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=true"

      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"

      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
      - "--certificatesresolvers.myresolver.acme.email=some-email@gmail.com"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
      - "--entrypoints.web.http.tls.certresolver=myresolver"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.stripprefix.stripprefix.prefixes=/traefik"
      - "traefik.http.routers.traefik.rule=Host(`domain.com.ar`) && (PathPrefix(`/traefik`) || Headers(`Referer`, `https://domain.com.ar/traefik/dashboard/`))"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.middlewares=traefik-auth, traefik-stripprefix, non_www, https_redirect"

        #two middleware declaration: https_redirect, that redirects http to https. And non_www, that redirects www.domain.com.ar to domain.com.ar
      - "traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`)"
      - "traefik.http.routers.http_catchall.entrypoints=web"
      - "traefik.http.routers.http_catchall.middlewares=https_redirect"
      - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https"
      - "traefik.http.middlewares.https_redirect.redirectscheme.permanent=true"
      - "traefik.http.middlewares.non_www.redirectregex.regex=^(http|https)://(?:www\\.)?(.+)"
      - "traefik.http.middlewares.non_www.redirectregex.replacement=https://$${2}"
      - "traefik.http.middlewares.non_www.redirectregex.permanent=true"

  web:
    image: nginx:latest
    restart: always
    volumes:
      - ../ofivirtual/src/oficina-virtual/www/:/usr/share/nginx/html
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.nginx.rule=Host(`domain.com.ar`)"
      - "traefik.http.routers.nginx.entrypoints=websecure"
      - "traefik.http.routers.nginx.tls.certresolver=myresolver"

      - "traefik.http.routers.nginx.middlewares=https_redirect@docker,non_www@docker"

```

I just want to know if this is the correct approach or if I'm missing something important.

---

<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:** [June 1, 2023, 6:41pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789/2 "2023-06-01T18:41:04Z")

</div>

How about going the easy route and setting the redirect on entrypoint? ([Doc](https://doc.traefik.io/traefik/routing/entrypoints/#redirection))

---

<div class="post-metadata">

**Author:** ![maurooyhanart](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@maurooyhanart](https://community.traefik.io/u/maurooyhanart)\
**Post date:** [June 2, 2023, 3:40pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789/3 "2023-06-02T15:40:51Z")

</div>

I tried that. What happens then is, page goes to a 404 not found, and browser says 'Connection not secure'.  
Do you know why that could be? Maybe I am not configuring correctly the certificate for the http? Could that be necessary?  
Thank you in advance.

---

<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:** [June 2, 2023, 5:42pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789/4 "2023-06-02T17:42:16Z")

</div>

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

You can’t use `PathPrefix` with Traefik Dashboard, it expects to be on "root" path, use a sub-domain.

---

<div class="post-metadata">

**Author:** ![maurooyhanart](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@maurooyhanart](https://community.traefik.io/u/maurooyhanart)\
**Post date:** [June 2, 2023, 6:47pm UTC](https://community.traefik.io/t/cannot-get-global-http-to-https-redirection-to-work/18789/5 "2023-06-02T18:47:50Z")

</div>

Thank you for pointing that out, I appreciate it. However, that is not the main issue of this discussion.
