Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect www to non-www not working in Traefik 2

Tags:

docker

traefik

I sett up Traefik to manage my docker containers but I can redirect www to non-www for my domain. I've tried everything in the forums but no luck

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.node.entrypoints=http"
  - "traefik.http.routers.node.rule=Host(`awebsite.com`) || Host(`www.awebsite.com`)"
  - "traefik.http.middlewares.vk-https-redirect.redirectscheme.scheme=https"

  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://www.awebsite.com/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://awebsite.com/$${1}"

  - "traefik.http.routers.node.middlewares=vk-https-redirect"
  - "traefik.http.routers.node-secure.entrypoints=https"
  - "traefik.http.routers.node-secure.rule=Host(`awebsite.com`) || Host(`www.awebsite.com`)"
  - "traefik.http.routers.node-secure.tls=true"
  - "traefik.http.routers.node-secure.tls.certresolver=http"
  - "traefik.http.routers.node-secure.service=node"
  - "traefik.http.services.node.loadbalancer.server.port=8000"
  - "traefik.docker.network=proxy"
like image 455
webface Avatar asked Jan 25 '23 04:01

webface


1 Answers

It not enough to define the redirect middleware, you also need to use it with the defined routers

here is you middleware definitions

 - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://www.awebsite.com/(.*)"
 - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://awebsite.com/$${1}"

you may need to add this lable too

traefik.http.middlewares.test-redirectregex.redirectregex.permanent=true

but you never used it in any of the defined routers. For instance, if you want this to apply to node-secure router, you need to add this label

- "traefik.http.routers.node-secure.middlewares=test-redirectregex"

More information, details, and examples can be found in this article

like image 93
Al-waleed Shihadeh Avatar answered Jan 31 '23 08:01

Al-waleed Shihadeh