2017-03-15 01:22:02 +00:00
# Rewrite
2017-03-13 15:03:47 +00:00
2022-01-17 00:57:28 +00:00
This example demonstrates how to use `Rewrite` annotations.
2017-03-13 15:03:47 +00:00
## Prerequisites
2018-03-27 15:50:26 +00:00
You will need to make sure your Ingress targets exactly one Ingress
2018-04-24 10:11:47 +00:00
controller by specifying the [ingress.class annotation ](../../user-guide/multiple-ingress.md ),
2019-11-08 19:22:52 +00:00
and that you have an ingress controller [running ](../../deploy/ ) in your cluster.
2017-03-13 15:03:47 +00:00
## Deployment
Rewriting can be controlled using the following annotations:
|Name|Description|Values|
| --- | --- | --- |
2017-11-24 18:46:51 +00:00
|nginx.ingress.kubernetes.io/rewrite-target|Target URI where the traffic must be redirected|string|
2022-01-17 00:57:28 +00:00
|nginx.ingress.kubernetes.io/ssl-redirect|Indicates if the location section is only accessible via SSL (defaults to True when Ingress contains a Certificate)|bool|
2017-11-24 18:46:51 +00:00
|nginx.ingress.kubernetes.io/force-ssl-redirect|Forces the redirection to HTTPS even if the Ingress is not TLS Enabled|bool|
2022-01-17 00:57:28 +00:00
|nginx.ingress.kubernetes.io/app-root|Defines the Application Root that the Controller must redirect if it's in `/` context|string|
2018-10-01 17:54:11 +00:00
|nginx.ingress.kubernetes.io/use-regex|Indicates if the paths defined on an Ingress use regular expressions|bool|
2017-03-13 15:03:47 +00:00
2018-12-13 18:02:05 +00:00
## Examples
2017-03-13 15:03:47 +00:00
### Rewrite Target
2017-10-16 12:55:46 +00:00
2018-12-13 18:02:05 +00:00
!!! attention
2019-01-04 13:48:46 +00:00
Starting in Version 0.22.0, ingress definitions using the annotation `nginx.ingress.kubernetes.io/rewrite-target` are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a [capture group ](https://www.regular-expressions.info/refcapture.html ).
2019-11-08 19:22:52 +00:00
2018-12-13 18:02:05 +00:00
!!! note
2019-11-08 19:22:52 +00:00
[Captured groups ](https://www.regular-expressions.info/refcapture.html ) are saved in numbered placeholders, chronologically, in the form `$1` , `$2` ... `$n` . These placeholders can be used as parameters in the `rewrite-target` annotation.
2018-12-13 18:02:05 +00:00
2023-09-03 21:09:47 +00:00
!!! note
2024-03-06 05:49:14 +00:00
Please see the [FAQ ](../../faq.md#validation-of-path ) for Validation Of __ `path` __
2023-09-03 21:09:47 +00:00
2017-03-08 12:02:13 +00:00
Create an Ingress rule with a rewrite annotation:
2017-10-16 12:55:46 +00:00
```console
2019-04-05 17:06:00 +00:00
$ echo '
2021-08-21 20:42:00 +00:00
apiVersion: networking.k8s.io/v1
2017-03-08 12:02:13 +00:00
kind: Ingress
metadata:
annotations:
2023-02-15 13:50:24 +00:00
nginx.ingress.kubernetes.io/use-regex: "true"
2019-04-25 10:43:26 +00:00
nginx.ingress.kubernetes.io/rewrite-target: /$2
2017-03-08 12:02:13 +00:00
name: rewrite
namespace: default
spec:
2021-11-09 15:43:49 +00:00
ingressClassName: nginx
2017-03-08 12:02:13 +00:00
rules:
- host: rewrite.bar.com
http:
paths:
2021-11-02 00:12:58 +00:00
- path: /something(/|$)(.*)
2023-09-03 21:09:47 +00:00
pathType: ImplementationSpecific
2021-11-02 00:12:58 +00:00
backend:
service:
name: http-svc
port:
number: 80
2019-04-05 17:06:00 +00:00
' | kubectl create -f -
2017-03-08 12:02:13 +00:00
```
2019-11-08 19:22:52 +00:00
In this ingress definition, any characters captured by `(.*)` will be assigned to the placeholder `$2` , which is then used as a parameter in the `rewrite-target` annotation.
2017-03-08 12:02:13 +00:00
2018-12-13 18:02:05 +00:00
For example, the ingress definition above will result in the following rewrites:
2020-01-12 00:56:57 +00:00
2018-12-13 18:02:05 +00:00
- `rewrite.bar.com/something` rewrites to `rewrite.bar.com/`
- `rewrite.bar.com/something/` rewrites to `rewrite.bar.com/`
- `rewrite.bar.com/something/new` rewrites to `rewrite.bar.com/new`
2017-03-08 12:02:13 +00:00
2017-03-13 15:03:47 +00:00
### App Root
2020-12-07 09:32:26 +00:00
Create an Ingress rule with an app-root annotation:
2017-03-13 15:03:47 +00:00
```
$ echo "
2021-08-21 20:42:00 +00:00
apiVersion: networking.k8s.io/v1
2017-03-13 15:03:47 +00:00
kind: Ingress
metadata:
annotations:
2017-11-24 18:46:51 +00:00
nginx.ingress.kubernetes.io/app-root: /app1
2017-03-13 15:03:47 +00:00
name: approot
namespace: default
spec:
2021-11-09 15:43:49 +00:00
ingressClassName: nginx
2017-03-13 15:03:47 +00:00
rules:
- host: approot.bar.com
http:
paths:
2021-11-02 00:12:58 +00:00
- path: /
pathType: Prefix
backend:
service:
name: http-svc
port:
number: 80
2017-03-13 15:03:47 +00:00
" | kubectl create -f -
```
Check the rewrite is working
```
$ curl -I -k http://approot.bar.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 14:57:15 GMT
Content-Type: text/html
Content-Length: 162
2021-10-01 13:11:23 +00:00
Location: http://approot.bar.com/app1
2017-03-13 15:03:47 +00:00
Connection: keep-alive
2017-03-15 01:22:02 +00:00
```