RedirectRegex

Redirecting the Client to a Different Location

The RedirectRegex redirects a request using regex matching and replacement.

Configuration Examples

Docker

  1. # Redirect with domain replacement
  2. # Note: all dollar signs need to be doubled for escaping.
  3. labels:
  4. - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  5. - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"

Kubernetes

  1. # Redirect with domain replacement
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-redirectregex
  6. spec:
  7. redirectRegex:
  8. regex: ^http://localhost/(.*)
  9. replacement: http://mydomain/${1}

Consul Catalog

  1. # Redirect with domain replacement
  2. # Note: all dollar signs need to be doubled for escaping.
  3. - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  4. - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.test-redirectregex.redirectregex.regex": "^http://localhost/(.*)",
  3. "traefik.http.middlewares.test-redirectregex.redirectregex.replacement": "http://mydomain/${1}"
  4. }

Rancher

  1. # Redirect with domain replacement
  2. # Note: all dollar signs need to be doubled for escaping.
  3. labels:
  4. - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  5. - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"

File (YAML)

  1. # Redirect with domain replacement
  2. http:
  3. middlewares:
  4. test-redirectregex:
  5. redirectRegex:
  6. regex: "^http://localhost/(.*)"
  7. replacement: "http://mydomain/${1}"

File (TOML)

  1. # Redirect with domain replacement
  2. [http.middlewares]
  3. [http.middlewares.test-redirectregex.redirectRegex]
  4. regex = "^http://localhost/(.*)"
  5. replacement = "http://mydomain/${1}"

Configuration Options

Tip

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

permanent

Set the permanent option to true to apply a permanent redirection.

regex

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

replacement

The replacement option defines how to modify the URL to have the new target URL.

Warning

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