Middleware rule not working for router

I'm trying to create a redirect for a specific path, supporting both http and https as i have another middle that does catch all http request and forwards them to https.

  site:
    image: containous/whoami
    networks:
      - public
    deploy:
      labels:
        - "traefik.http.routers.helloworld.rule=Host(`jebster.net`)"
        - "traefik.http.routers.helloworld.entrypoints=websecure"
        - "traefik.http.routers.helloworld.tls.certresolver=le"
        - "traefik.http.services.helloworld.loadbalancer.server.port=80"
        # Tested and works here: https://play.golang.org/p/jS5pDAoJiQ_i
        - "traefik.http.middlewares.gamlihandil-redirect.replacepathregex.regex=^?jebster.net/gh/perbondi/(.*)"
        - "traefik.http.middlewares.gamlihandil-redirect.replacepathregex.replacement=gh.jebster.net/$$1"
        - "traefik.http.routers.helloworld.middlewares=gamlihandil-redirect"

It shows correctly on the Dashboard:

As seen on the picture you can correctly see the regex rule there. And here's a sample of what I want working: https://play.golang.org/p/jS5pDAoJiQ_i

Hello,

replacepathregex is made to replace the Path not the Host.

https://docs.traefik.io/v2.1/middlewares/replacepathregex/

Your router match the host jebster.net and you are trying to redirect to gh.jebster.net.
But you don't have any router that match gh.jebster.net.

You have to use redirectregex instead of replacepathregex

https://docs.traefik.io/v2.1/middlewares/redirectregex/

An simple example with self-signed cert and locahost's domains:

version: "3.7"

services:
  traefik:
    image: traefik:v2.1.1
    command: >
      --log.level=INFO
      --api
      --providers.docker.exposedbydefault=false
      --entrypoints.web.address=:80
      --entrypoints.websecure.address=:443
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      traefik.enable: true
      traefik.http.routers.traefik.rule: "Host(`traefik.localhost`)"
      traefik.http.routers.traefik.entrypoints: web
      traefik.http.routers.traefik.service: api@internal

  whoami:
    image: containous/whoami
    labels:
      traefik.enable: true

      traefik.http.routers.helloworld.rule: HostRegexp(`{sub:(gh\.)?}jebster.localhost`)
      traefik.http.routers.helloworld.entrypoints: websecure
      traefik.http.routers.helloworld.tls: true
      # traefik.http.routers.helloworld.tls.certresolver: le
      # traefik.http.routers.helloworld.tls.domains[0].main: jebster.localhost
      # traefik.http.routers.helloworld.tls.domains[0].sans: gh.jebster.localhost
      traefik.http.routers.helloworld.middlewares: gamlihandil-redirect
      
      traefik.http.middlewares.gamlihandil-redirect.redirectregex.regex: ^https://jebster\.localhost/gh/perbondi/(.*)
      traefik.http.middlewares.gamlihandil-redirect.redirectregex.replacement: https://gh.jebster.localhost/$$1
1 Like

That works. But this broke the other router that I have deployed which has:
"traefik.http.routers.gamlihandil.rule=Host(gh.jebster.net)"

Got it working with:

 site:
    image: containous/whoami
    networks:
      - public
    deploy:
      labels:
        - "traefik.http.routers.helloworld.rule=Host(`jebster.net`)"
        - "traefik.http.routers.helloworld.entrypoints=websecure"
        - "traefik.http.routers.helloworld.tls.certresolver=le"
        - "traefik.http.services.helloworld.loadbalancer.server.port=80"
        - "traefik.http.middlewares.gamlihandil-redirect.redirectregex.regex=^https://jebster\\.net/gh/perbondi/(.*)"
        - "traefik.http.middlewares.gamlihandil-redirect.redirectregex.replacement=https://gh.jebster.net/$$1"
        - "traefik.http.routers.helloworld.middlewares=gamlihandil-redirect"

Thanks for the help