Site Redirection in Traefik using RedirectRegex Middleware

Nov 6, 2019·
ZeroExistence
ZeroExistence
· 1 min read

Implementing URL redirection in Traefik works like normal service with middleware processing. To setup redirection, setup the base router with middleware, and some dummy service.

http:
  routers:
    shop-secure:
      entryPoints:
      - "web-fe-secure"
      rule: "Host(`shop.moe.ph`)"
      middlewares:
      - "shopredirect"
      service: "shop-redirect"
      tls: {}

For middleware, you will need to use redirectRegex middleware processor. Just put the URL that you want to process, and it’s replacement.

http:
  middlewares:
    shopredirect:
      redirectRegex:
        regex: "^https://shop.moe.ph/(.*)"
        replacement: "https://shopee.ph/moe_ph"

Unfortunately, this router-middleware processing of URL redirection still needs service block. If you are redirecting your URL to another domain, just put a dummy service.

http:
  services:
    shop-redirect:
      loadBalancer:
        servers:
        - url: "http://localhost/"

To do more complex redirection, just check the Traefik’s RedirectRegex documentation.