This project is in the process of being donated to the CNCF and is not affiliated with the Kubernetes project.

Path rewrites

Rewrite path prefixes in requests.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install K8sGateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n gloo-system gloo-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/gloo-proxy-http -n gloo-system 8080:8080

Rewrite prefix path

Path rewrites use the HTTP path modifier to rewrite path prefixes.

  1. 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
  2. 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 the rewrite.example domain are rewritten according to the rules that are defined in the RouteOption resource.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    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
  3. Send a request to the httpbin app along the /headers path on the rewrite.example domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the /anything path.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/headers -H "host: rewrite.example:8080"
    curl -vik localhost:8080/headers -H "host: rewrite.example"

    Example output:

    ...
    "origin": "10.0.9.36:50660",
    "url": "http://rewrite.example:8080/anything",
    "data": "",
    "files": null,
    "form": null,
    "json": null
    ...
  4. 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

  1. 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 with anything.

    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
  2. 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 the rewrite.example domain are evaluated against the regex pattern that you defined in the RouteOption resource.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    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
  3. Send a request to the httpbin app along the /headers path on the rewrite.example domain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the /anything path.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/headers -H "host: rewrite.example:8080"
    curl -vik localhost:8080/headers -H "host: rewrite.example"

    Example output:

    ...
    "origin": "10.0.9.36:50660",
    "url": "http://rewrite.example:8080/anything",
    "data": "",
    "files": null,
    "form": null,
    "json": null
    ...
  4. Optional: Clean up the resources that you created.

    kubectl delete routeoption rewrite -n httpbin
    kubectl delete httproute httpbin-rewrite -n httpbin