Skip to content
agentgateway has joined the Agentic AI Foundation — Learn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Host rewrites

Verified : Code examples on this page have been automatically tested and verified.
Page as Markdown

Replace the host header value before forwarding a request to a backend service.

Replace the host header value before forwarding a request to a backend service by using the URLRewrite filter.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Set up an agentgateway proxy.
  2. Install the httpbin sample app.

In-cluster service host rewrites

  1. Create an HTTPRoute resource for the httpbin app that uses the URLRewrite filter to rewrite the hostname of the request. In this example, all incoming requests on the rewrite.example domain are rewritten to the www.example.com host.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-rewrite
      namespace: httpbin
    spec:
      parentRefs:
      - name: agentgateway-proxy
        namespace: agentgateway-system
      hostnames:
        - rewrite.example
      rules:
        - matches:
            - path:
                type: PathPrefix
                value: /
          filters:
            - type: URLRewrite
              urlRewrite:
                hostname: "www.example.com"
          backendRefs:
            - name: httpbin
              port: 8000
    EOF
    SettingDescription
    spec.parentRefsThe name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the agentgateway-proxy gateway that was created as part of the get started guide.
    spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the URLRewrite filter is used.
    spec.rules.filters.urlRewrite.hostnameThe hostname that you want to rewrite requests to.
    spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide.
  2. Send a request to the httpbin app on the rewrite.example domain. Verify that you get back a 200 HTTP response code and that you see the Host: www.example.com header in your response.

    Note

    The following request returns a 200 HTTP response code, because you set up an HTTPRoute for the httpbin app on the www.example.com domain as part of the getting started guide. If you chose a different domain for your example, make sure that you have an HTTPRoute that can be reached under the host you want to rewrite to.

    curl -vi http://$INGRESS_GW_ADDRESS:80/headers -H "host: rewrite.example:80"

    Example output:

    ...
    {
     "headers": {
       "Accept": [
         "*/*"
       ],
       "Host": [
         "www.example.com"
       ],
       "User-Agent": [
         "curl/8.7.1"
       ]
     }
    }
    

External service host rewrites

  1. Create an AgentgatewayBackend that represents your external service. The following example creates an AgentgatewayBackend for the httpbin.org domain.

    kubectl apply -f- <<EOF
    apiVersion: agentgateway.dev/v1alpha1
    kind: AgentgatewayBackend
    metadata:
      name: httpbin
      namespace: httpbin
    spec:
      static:
        host: httpbin.org
        port: 80
    EOF
  2. Create an HTTPRoute resource that matches incoming traffic on the external-rewrite.example domain and forwards traffic to the AgentgatewayBackend that you created. Because the AgentgatewayBackend expects a different domain, you use the URLRewrite filter to rewrite the hostname from external-rewrite.example to httpbin.org.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: backend-rewrite
      namespace: httpbin
    spec:
      parentRefs:
      - name: agentgateway-proxy
        namespace: agentgateway-system
      hostnames:
        - external-rewrite.example
      rules:
         - filters:
           - type: URLRewrite
             urlRewrite:
               hostname: "httpbin.org"
           backendRefs:
           - name: httpbin
             kind: AgentgatewayBackend
             group: agentgateway.dev
    EOF
  3. Send a request to the external-rewrite.example domain. Verify that you get back a 200 HTTP response code and that you see the Host: httpbin.org header in your response.

    curl -vi http://$INGRESS_GW_ADDRESS:80/headers -H "host: external-rewrite.example:80"

    Example output:

    * Request completely sent off
    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    < content-type: application/json
    content-type: application/json
    < content-length: 268
    content-length: 268
    < server: envoy
    server: envoy
    < access-control-allow-origin: *
    access-control-allow-origin: *
    < access-control-allow-credentials: true
    access-control-allow-credentials: true
    < x-envoy-upstream-service-time: 2416
    x-envoy-upstream-service-time: 2416
    < 
    
    {
      "headers": {
        "Accept": "*/*", 
        "Host": "httpbin.org", 
        "User-Agent": "curl/8.7.1"
      }
    }   
    

Backend-specific host rewrites

When a rule splits traffic between backends that expect different hosts, rewrite the host for one backend only. To do so, add the URLRewrite filter to the backendRefs entry of that backend instead of to the rule. Requests to the other backends in the rule keep their original host.

  1. Create an HTTPRoute that splits traffic on the backend-host-rewrite.example domain between the in-cluster httpbin app and the httpbin.org AgentgatewayBackend that you created in the external service section. Only the requests to httpbin.org are rewritten to the httpbin.org host.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: backend-host-rewrite
      namespace: httpbin
    spec:
      parentRefs:
      - name: agentgateway-proxy
        namespace: agentgateway-system
      hostnames:
        - backend-host-rewrite.example
      rules:
        - backendRefs:
            - name: httpbin
              port: 8000
              weight: 50
            - name: httpbin
              kind: AgentgatewayBackend
              group: agentgateway.dev
              weight: 50
              filters:
                - type: URLRewrite
                  urlRewrite:
                    hostname: "httpbin.org"
    EOF
    SettingDescription
    spec.rules.backendRefs.weightThe share of requests that each backend gets. In this example, each backend gets half of the requests.
    spec.rules.backendRefs.filters.typeThe filter that applies only to the requests that the gateway forwards to this backend. In this example, the URLRewrite filter is used. The in-cluster httpbin backend has no filter, so its requests keep the backend-host-rewrite.example host.
    spec.rules.backendRefs.filters.urlRewrite.hostnameThe hostname that you want to rewrite to for the requests that the gateway forwards to this backend.
  2. Send six requests to the backend-host-rewrite.example domain, and print the Host header that each backend received. Verify that the requests to the in-cluster httpbin app keep the backend-host-rewrite.example host, and that the requests to httpbin.org have the httpbin.org host.

    for i in $(seq 1 6); do
      curl -s http://$INGRESS_GW_ADDRESS:80/headers -H "host: backend-host-rewrite.example" | jq -c '.headers.Host'
    done

    Example output: The in-cluster httpbin app returns the host as a list, and httpbin.org returns it as a string. Because the gateway splits the traffic by weight, the order of your output differs.

    ["backend-host-rewrite.example"]
    "httpbin.org"
    "httpbin.org"
    ["backend-host-rewrite.example"]
    "httpbin.org"
    ["backend-host-rewrite.example"]
    

Cleanup

You can remove the resources that you created in this guide.
kubectl delete httproute httpbin-rewrite -n httpbin
kubectl delete httproute backend-rewrite -n httpbin
kubectl delete AgentgatewayBackend httpbin -n httpbin

If you created the backend-specific host rewrite, delete its HTTPRoute.

kubectl delete httproute backend-host-rewrite -n httpbin
Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate ↵ select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.