RedirectRegex

将客户端重定向到其他位置

RegexRedirect使用regex匹配和替换将请求从URL重定向到另一个.

Configuration Examples

# Redirect with domain replacement
# Note: all dollar signs need to be doubled for escaping.
labels:
  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
# Redirect with domain replacement
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-redirectregex
spec:
  redirectRegex:
    regex: ^http://localhost/(.*)
    replacement: http://mydomain/${1}
"labels": {
  "traefik.http.middlewares.test-redirectregex.redirectregex.regex": "^http://localhost/(.*)",
  "traefik.http.middlewares.test-redirectregex.redirectregex.replacement": "http://mydomain/${1}"
}
# Redirect with domain replacement
# Note: all dollar signs need to be doubled for escaping.
labels:
  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
# Redirect with domain replacement
[http.middlewares]
  [http.middlewares.test-redirectregex.redirectRegex]
    regex = "^http://localhost/(.*)"
    replacement = "http://mydomain/${1}"
# Redirect with domain replacement
http:
  middlewares:
    test-redirectregex:
      redirectRegex:
        regex: "^http://localhost/(.*)"
        replacement: "http://mydomain/${1}"

Configuration Options

permanent

permanent选项设置为true以应用永久重定向.

regex

regex选项是正则表达式,用于匹配和捕获请求URL中的元素.

Warning

定义替换扩展变量时应小心: $1x等效于${1x} ,而不是${1}x (请参见Regexp.Expand ),因此请使用${1}语法.

Tip

可以使用Go PlaygroundRegex101等在线工具测试正则表达式和替换 .

replacement

replacement选项定义了如何将URL修改为具有新的目标URL.