Path rewrites
Rewrite path prefixes in requests.
For more information, see the Kubernetes Gateway API documentation.
Before you begin
Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Rewrite prefix path
Path rewrites use the HTTP path modifier to rewrite path prefixes.
Create a RouteOption resource to define your rewrite rules. In the following example all incoming request paths are rewritten to the
/anything
path.kubectl apply -n httpbin -f- <<EOF apiVersion: gateway.solo.io/v1 kind: RouteOption metadata: name: rewrite namespace: httpbin spec: options: prefixRewrite: '/anything' EOF
Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests that match the
/headers
path on therewrite.example
domain are rewritten according to the rules that are defined in the RouteOption resource.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1beta1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - rewrite.example rules: - matches: - path: type: Exact value: /headers filters: - type: ExtensionRef extensionRef: group: gateway.solo.io kind: RouteOption name: rewrite backendRefs: - name: httpbin port: 8000 EOF
Send a request to the httpbin app along the
/headers
path on therewrite.example
domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the/anything
path.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything", "data": "", "files": null, "form": null, "json": null ...
Optional: Clean up the resources that you created.
kubectl delete routeoption rewrite -n httpbin kubectl delete httproute httpbin-rewrite -n httpbin
Rewrite full or prefix path with regex
Create a RouteOption resource to define your rewrite rules. In the following example all incoming request paths are evaluated against the regex pattern. If
headers
is part of the request path, it is replaced withanything
.kubectl apply -n httpbin -f- <<EOF apiVersion: gateway.solo.io/v1 kind: RouteOption metadata: name: rewrite namespace: httpbin spec: options: regexRewrite: pattern: regex: 'headers' substitution: 'anything' EOF
Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests along the
/headers
path on therewrite.example
domain are evaluated against the regex pattern that you defined in the RouteOption resource.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1beta1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - rewrite.example rules: - matches: - path: type: Exact value: /headers filters: - type: ExtensionRef extensionRef: group: gateway.solo.io kind: RouteOption name: rewrite backendRefs: - name: httpbin port: 8000 EOF
Send a request to the httpbin app along the
/headers
path on therewrite.example
domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the/anything
path.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything", "data": "", "files": null, "form": null, "json": null ...
Optional: Clean up the resources that you created.
kubectl delete routeoption rewrite -n httpbin kubectl delete httproute httpbin-rewrite -n httpbin