2022-04-15 13:44:08 +00:00
---
title: "Traefik ReplacePathRegex Documentation"
description: "In Traefik Proxy's HTTP middleware, ReplacePathRegex updates paths before forwarding requests, using a regex. Read the technical documentation."
---
2019-04-08 15:14:08 +00:00
# ReplacePathRegex
2019-02-26 13:50:07 +00:00
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
2021-02-11 13:34:04 +00:00
The ReplaceRegex replaces the path of a URL using regex matching and replacement.
2019-04-08 15:14:08 +00:00
## 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/(.*)"
2019-11-05 12:22:04 +00:00
- "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
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
# Replace path with regex
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"
```
2019-04-15 16:22:07 +00:00
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.test-replacepathregex.replacepathregex.regex": "^/foo/(.*)",
"traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement": "/bar/$1"
}
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
2021-02-11 13:34:04 +00:00
# Replace path with regex
2019-07-22 07:58:04 +00:00
http:
middlewares:
test-replacepathregex:
replacePathRegex:
regex: "^/foo/(.*)"
replacement: "/bar/$1"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
# Replace path with regex
[http.middlewares]
[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:
2021-02-11 13:34:04 +00:00
- replace the matching path with the specified one.
2019-04-08 15:14:08 +00:00
- store the original path in a `X-Replaced-Path` header.
2021-02-11 13:34:04 +00:00
!!! tip
2021-10-06 09:36:11 +00:00
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 ).
When defining a regular expression within YAML, any escaped character needs to be escaped twice: `example\.com` needs to be written as `example\\.com` .
2021-02-11 13:34:04 +00:00
2019-04-08 15:14:08 +00:00
### `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.
2021-06-18 22:08:08 +00:00
2021-02-11 13:34:04 +00:00
### `replacement`
The `replacement` option defines the replacement path format, which can include captured variables.
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.