traefik/docs/content/middlewares/replacepathregex.md

89 lines
2.3 KiB
Markdown
Raw Normal View History

2019-04-08 15:14:08 +00:00
# ReplacePathRegex
Updating the Path Before Forwarding the Request (Using a Regex)
2019-04-08 15:14:08 +00:00
{: .subtitle }
2019-09-10 12:40:05 +00:00
<!--
TODO: add schema
-->
2019-04-08 15:14:08 +00:00
The ReplaceRegex replace a path from an url to another with regex matching and replacement.
## Configuration Examples
```yaml tab="Docker"
# Replace path with regex
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"
2019-04-08 15:14:08 +00:00
```
```yaml tab="Kubernetes"
# Replace path with regex
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: test-replacepathregex
spec:
replacePathRegex:
regex: ^/foo/(.*)
replacement: /bar/$1
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.test-replacepathregex.replacepathregex.regex": "^/foo/(.*)",
"traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement": "/bar/$1"
}
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
# Replace path with regex
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"
2019-04-08 15:14:08 +00:00
```
2019-07-22 07:58:04 +00:00
```toml tab="File (TOML)"
2019-04-08 15:14:08 +00:00
# Redirect with domain replacement
[http.middlewares]
[http.middlewares.test-replacepathregex.replacePathRegex]
regex = "^/foo/(.*)"
replacement = "/bar/$1"
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
# Redirect with domain replacement
http:
middlewares:
test-replacepathregex:
replacePathRegex:
regex: "^/foo/(.*)"
replacement: "/bar/$1"
```
2019-04-08 15:14:08 +00:00
## Configuration Options
### General
The ReplacePathRegex middleware will:
- replace the matching path by the specified one.
- store the original path in a `X-Replaced-Path` header.
### `regex`
2019-07-01 09:30:05 +00:00
The `regex` option is the regular expression to match and capture the path from the request URL.
2019-04-08 15:14:08 +00:00
!!! warning
Care should be taken when defining replacement expand variables: `$1x` is equivalent to `${1x}`, not `${1}x` (see [Regexp.Expand](https://golang.org/pkg/regexp/#Regexp.Expand)), so use `${1}` syntax.
!!! tip
Regular expressions and replacements can be tested using online tools such as [Go Playground](https://play.golang.org/p/mWU9p-wk2ru) or the [Regex101](https://regex101.com/r/58sIgx/2).
### `replacement`
The `replacement` option defines how to modify the path to have the new target path.