Set up URL rewrite for a classic Application Load Balancer
Stay organized with collections
Save and categorize content based on your preferences.
This example demonstrates rewriting the path specified in a request for
a classic Application Load Balancer.
To configure traffic management for global external Application Load Balancers and
regional external Application Load Balancers, see the following pages:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 UTC."],[],[],null,["# Set up URL rewrite for a classic Application Load Balancer\n\nThis example demonstrates rewriting the path specified in a request for\na classic Application Load Balancer.\n\nTo configure traffic management for global external Application Load Balancers and\nregional external Application Load Balancers, see the following pages:\n\n- [Setting up traffic management for\n global external Application Load Balancer](/load-balancing/docs/https/setting-up-global-traffic-mgmt#rewrite-url)\n- [Setting up traffic management for\n regional external Application Load Balancer](/load-balancing/docs/https/setting-up-reg-traffic-mgmt#rewrite-url)\n\n### Before you begin\n\n- Read about [URL rewrites](/load-balancing/docs/https/traffic-management#rewrites).\n\n- Read about [URL maps in general](/load-balancing/docs/url-map-concepts) and\n [path matchers](/load-balancing/docs/url-map-concepts#pm-constraints)\n in particular.\n\n- The URL rewrite example assumes that you've already created your\n external Application Load Balancer following the steps outlined in [Setting up a load balancer\n with Cloud Storage\n buckets](/load-balancing/docs/https/ext-load-balancer-backend-buckets).\n\n That example leads you through the creation of two resources:\n - `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/never-fetch/three-cats.jpg`\n - `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/love-to-fetch/two-dogs.jpg`\n\n Where `/never-fetch/three-cats.jpg` is stored in `/cats`, which is\n the default service, and `/love-to-fetch/two-dogs.jpg` is stored in\n `/dogs`.\n\n At this point, the URL map looks like this: \n\n ```\n gcloud compute url-maps describe http-lb\n ``` \n\n creationTimestamp: '2020-10-13T11:18:10.561-07:00'\n defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n fingerprint: MKfYsObzqgw=\n hostRules:\n - hosts:\n - '*'\n pathMatcher: path-matcher-1\n id: '1420501688756228493'\n kind: compute#urlMap\n name: test-bucket\n pathMatchers:\n - defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n name: path-matcher-1\n pathRules:\n - paths:\n - /love-to-fetch/*\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n selfLink: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/urlMaps/http-lb\n\n### Modifying the URL map\n\n| **Important:** The rewrite is prepended to the path as is. Full path rewrites are not supported. External Application Load Balancers only implement path prefix rewrites. For example, you *can* rewrite: `host.name/path1/resource1` to `host.name/path2/resource1`. You *cannot* rewrite `host.name/path1/resource1` to `host.name/path1/resource2`.\n\nIn this example, you rewrite the `/love-to-fetch/` URL so that users can reach the\n`two-dogs.jpg` image with this simplified URL:\n\n- `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/two-dogs.jpg`\n\nTo do this, modify the load balancer's URL map as follows: \n\n### Console\n\n### Edit your load balancer\n\n1. Go to the Load balancing page in the Google Cloud console. \n [Go to the Load balancing page](https://console.cloud.google.com/networking/loadbalancing/add)\n2. Click `http-lb`.\n3. Click **Edit** edit.\n4. Keep the window open to continue.\n\n### Change the host and path rules\n\n1. In the left column of the screen, click **Host and path rules**.\n2. Select **Advanced host and path rule (URL redirect, URL rewrite)**.\n3. Click the row that contains the non-default path rule, in this case, the row that has an asterisk (`*`) for all hosts.\n4. Click the pencil icon edit for the `/love-to-fetch/* Route traffic to a single backend: dogs` row.\n5. Under **Paths** , delete `/love-to-fetch/*` and add `/*`.\n6. Under **Action** , select **Route traffic to a single backend**.\n7. Click **Add-on action (URL rewrite)**.\n8. Leave **Host rewrite** blank.\n9. Under **Path prefix rewrite** , enter `/love-to-fetch/`.\n10. Under **Backend** , select `dogs`.\n11. Click **Save**.\n12. Click **Done**.\n The new host and path rule looks as follows:\n\n \u003cbr /\u003e\n\n13. If everything looks correct, click **Update** to update your HTTP\n load balancer.\n\n### gcloud\n\n1. Create a YAML file `/tmp/http-lb.yaml`, making sure to substitute\n \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your project ID.\n\n When a user requests path `/*`, the path gets rewritten in the backend to\n the actual location of the content, which is `/love-to-fetch/*`. \n\n defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n hostRules:\n - hosts:\n - '*'\n pathMatcher: path-matcher-1\n name: http-lb\n pathMatchers:\n - defaultService: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/cats\n name: path-matcher-1\n pathRules:\n - paths:\n - /*\n routeAction:\n urlRewrite:\n pathPrefixRewrite: /love-to-fetch/\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n tests:\n - description: Test routing to backend bucket, dogs\n host: example.com\n path: /love-to-fetch/test\n service: https://www.googleapis.com/compute/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/global/backendBuckets/dogs\n\n2. Validate the URL map.\n\n ```\n gcloud compute url-maps validate --source /tmp/http-lb.yaml\n ```\n\n If the tests pass and the command outputs a success message,\n save the changes to the URL map.\n3. Update the URL map.\n\n ```\n gcloud compute url-maps import http-lb \\\n --source /tmp/http-lb.yaml \\\n --global\n ```\n\n### Testing\n\nNote the IPv4 address that was reserved: \n\n```\ngcloud compute addresses describe example-ip \\\n --format=\"get(address)\" \\\n --global\n```\n\nAfter waiting several minutes for the changes to propagate, you can test this\nsetup.\n\nOn the command line, run the following curl command: \n\n```\ncurl http://IP_ADDRESS/two-dogs.jpg\n```\n\nIn a browser, open `http://`\u003cvar translate=\"no\"\u003eIP_ADDRESS\u003c/var\u003e`/two-dogs.jpg`.\n\nWhat's next\n-----------\n\n- [Clean up the load balancer setup](/load-balancing/docs/cleaning-up-lb-setup)."]]