traefik/docs/content/middlewares/http/redirectregex.md

87 lines
2.6 KiB
Markdown
Raw Normal View History

---
title: "Traefik RedirectRegex Documentation"
description: "In Traefik Proxy's HTTP middleware, RedirectRegex redirecting clients to different locations. Read the technical documentation."
---
2019-04-08 15:14:08 +00:00
# RedirectRegex
Redirecting the Client to a Different Location
{: .subtitle }
2019-09-10 12:40:05 +00:00
<!--
TODO: add schema
-->
2021-02-11 13:34:04 +00:00
The RedirectRegex redirects a request using regex matching and replacement.
## Configuration Examples
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-04-03 12:32:04 +00:00
# Redirect with domain replacement
2019-09-09 08:36:08 +00:00
# Note: all dollar signs need to be doubled for escaping.
2019-04-03 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
- "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
2019-04-03 12:32:04 +00:00
```
```yaml tab="Kubernetes"
# Redirect with domain replacement
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-04-03 12:32:04 +00:00
kind: Middleware
metadata:
name: test-redirectregex
spec:
redirectRegex:
regex: ^http://localhost/(.*)
replacement: http://mydomain/${1}
2019-04-03 12:32:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
# Redirect with domain replacement
# Note: all dollar signs need to be doubled for escaping.
- "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
- "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
# Redirect with domain replacement
http:
middlewares:
test-redirectregex:
redirectRegex:
regex: "^http://localhost/(.*)"
replacement: "http://mydomain/${1}"
2019-07-22 07:58:04 +00:00
```
```toml tab="File (TOML)"
# Redirect with domain replacement
[http.middlewares]
[http.middlewares.test-redirectregex.redirectRegex]
regex = "^http://localhost/(.*)"
replacement = "http://mydomain/${1}"
```
## Configuration Options
2019-04-03 12:32:04 +00:00
### `permanent`
Set the `permanent` option to `true` to apply a permanent redirection.
2019-04-03 12:32:04 +00:00
### `regex`
2019-07-01 09:30:05 +00:00
The `regex` option is the regular expression to match and capture elements from the request URL.
!!! 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).
When defining a regular expression within YAML, any escaped character needs to be escaped twice: `example\.com` needs to be written as `example\\.com`.
2019-04-03 12:32:04 +00:00
### `replacement`
The `replacement` option defines how to modify the URL to have the new target URL.
2021-02-11 13:34:04 +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.