ReplacePathRegex

    The ReplaceRegex replaces the path of a URL using regex matching and replacement.

    Docker

    Kubernetes

    1. # Replace path with regex
    2. apiVersion: traefik.io/v1alpha1
    3. kind: Middleware
    4. name: test-replacepathregex
    5. spec:
    6. replacePathRegex:
    7. regex: ^/foo/(.*)
    8. replacement: /bar/$1

    Consul Catalog

    1. "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex": "^/foo/(.*)",
    2. "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement": "/bar/$1"

    Rancher

    File (YAML)

    1. # Replace path with regex
    2. http:
    3. middlewares:
    4. test-replacepathregex:
    5. replacePathRegex:
    6. regex: "^/foo/(.*)"
    7. replacement: "/bar/$1"

    File (TOML)

    Configuration Options

    The ReplacePathRegex middleware will:

    • replace the matching path with the specified one.

    Regular expressions and replacements can be tested using online tools such as or the Regex101.

    When defining a regular expression within YAML, any escaped character needs to be escaped twice: example\.com needs to be written as example\\.com.

    The regex option is the regular expression to match and capture the path from the request URL.

    The replacement option defines the replacement path format, which can include captured variables.

    Care should be taken when defining replacement expand variables: $1x is equivalent to ${1x}, not ${1}x (see ), so use ${1} syntax.