🐦‍⬛Documentation

Regex Redirecting

Configure regex-based redirects in Traefik

Redirecting a URL to another can be done with a middleware in the Traefik config. First, make an entry point with a dummy service:

Due to Traefik's structure, the service must exist, or the config will reject it.

In this instance, I am using my local jellyfin redirect, as it doesn't expose anything if something were to happen.

     page-redirect: # redirect
       entryPoints:
         - https
       rule: 'Host(`domain.xyz`)'
       service: jellyfin-local # any service will do as we are just redirecting
       middlewares:
         - "redirect"

Once the entry point is configured, we need to set the middleware to handle what happens next:

Middleware

Preserve Path

To redirect domain.xyz/s/custom-url to domain.com/s/custom-url, use the following regex:

    redirect:
      redirectRegex:
      # (.*) captures the full path
        regex: "^https?://domain\\.xyz(/.*)?$"
        replacement: "https://wiki.domain.xyz$1"
        permanent: true

Root Redirect

To redirect all traffic e.g. domain.xyz/* to domain.com, use the following regex:

    redirect:
      redirectRegex:
      # (.*) captures the full path
        regex: ".*"
        replacement: "https://domain.com"
        permanent: true

On this page