Path redirects
Redirect requests to a different path prefix.
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.
Set up path redirects
Path redirects use the HTTP path modifier to replace either an entire path or path prefixes.
Replace prefix path
Create an HTTP route for the httpbin app that you set up as part of the Get started guide. In the following example, requests to the
/getpath are redirected to the/status/200path, and a 302 HTTP response code is returned to the user.Because the
ReplacePrefixPathpath modifier is used, only the path prefix is replaced during the redirect. For example, requests tohttp://path.redirect.example/getresult in thehttps://path.redirect.example/status/200redirect location. However, for longer paths, such as inhttp://path.redirect.example/get/headers, only the prefix is replaced and a redirect location ofhttps://path.redirect.example/status/200/headersis returned.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-redirect namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - path.redirect.example rules: - matches: - path: type: PathPrefix value: /get filters: - type: RequestRedirect requestRedirect: path: type: ReplacePrefixMatch replacePrefixMatch: /status/200 statusCode: 302 EOFSetting Description spec.parentRefs.nameThe name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide. spec.hostnamesThe hostname for which you want to apply the redirect. spec.rules.matches.pathThe prefix path that you want to redirect. In this example, you want to redirect all requests to the /getpath.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirectis used.spec.rules.filters.requestRedirect.path.typeThe type of path modifier that you want to apply. In this example, you want to replace only the prefix path. spec.rules.filters.requestRedirect.path.replacePrefixPathThe prefix path you want to redirect to. In this example, all requests to the /getprefix path are redirected to the/status/200prefix path.spec.rules.filters.requestRedirect.statusCodeThe HTTP status code that you want to return to the client in case of a redirect. For non-permanent redirects, use the 302 HTTP response code. Send a request to the httpbin app on the
path.redirect.exampledomain. Verify that you get back a 302 HTTP response code andpath.redirect.example:8080/status/200redirect location.Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < date: Mon, 06 Nov 2023 01:51:00 GMT date: Mon, 06 Nov 2023 01:51:00 GMT < server: envoy server: envoy < content-length: 0 content-length: 0Send another request to the httpbin app on the
path.redirect.exampledomain. This time, you send the request to the/get/headerspath. Verify that you get back a 302 HTTP response code and the redirect locationpath.redirect.example:8080/status/200/headers.Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200/headers location: http://path.redirect.example:8080/status/200/headers < date: Mon, 06 Nov 2023 01:53:56 GMT date: Mon, 06 Nov 2023 01:53:56 GMT < server: envoy server: envoy < content-length: 0 content-length: 0
Replace full path
Create an HTTP route for the httpbin app that you set up as part of the Get started guide. In the following example, requests to the
/getpath are redirected to the/status/200path, and a 302 HTTP response code is returned to the user.Because the
ReplaceFullPathpath modifier is used, requests tohttp://path.redirect.example/getandhttp://path.redirect.example/get/headersboth receivehttps://path.redirect.example/status/200as the redirect location.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-redirect namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - path.redirect.example rules: - matches: - path: type: PathPrefix value: /get filters: - type: RequestRedirect requestRedirect: path: type: ReplaceFullPath replaceFullPath: /status/200 statusCode: 302 EOFSetting Description spec.parentRefs.nameThe name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide. spec.hostnamesThe hostname for which you want to apply the redirect. spec.rules.matches.pathThe prefix path that you want to redirect. In this example, you want to redirect all requests to the /getpath.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirectis used.spec.rules.filters.requestRedirect.path.typeThe type of path modifier that you want to apply. In this example, you want to replace the full path.. spec.rules.filters.requestRedirect.path.replaceFullPathThe path that you want to redirect to. In this example, all requests to the /getprefix path are redirected to the/status/200prefix path.spec.rules.filters.requestRedirect.statusCodeThe HTTP status code that you want to return to the client in case of a redirect. For non-permanent redirects, use the 302 HTTP response code. Send a request to the httpbin app on the
path.redirect.exampledomain. Verify that you get back a 302 HTTP response code and the redirect locationpath.redirect.example:8080/status/200.Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < date: Mon, 06 Nov 2023 01:55:14 GMT date: Mon, 06 Nov 2023 01:55:14 GMT < server: envoy server: envoy < content-length: 0 content-length: 0Send another request to the httpbin app on the
path.redirect.exampledomain. This time, you send the request to the/get/headerspath. Verify that you get back a 302 HTTP response code and the same redirect location as in the previous example (path.redirect.example:8080/status/200).Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < date: Mon, 06 Nov 2023 01:56:06 GMT date: Mon, 06 Nov 2023 01:56:06 GMT < server: envoy server: envoy < content-length: 0 content-length: 0
Cleanup
You can optionally delete the HTTP route that you created as part of this guide.
kubectl delete httproute httpbin-redirect -n httpbin