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 fora classic Application Load Balancer.
To configure traffic management for global external Application Load Balancers andregional external Application Load Balancers, see the following pages:
- Setting up traffic management forglobal external Application Load Balancer
- Setting up traffic management forregional external Application Load Balancer
Before you begin
Read aboutURL rewrites.
Read aboutURL maps in general andpath matchersin particular.
The URL rewrite example assumes that you've already created yourexternal Application Load Balancer following the steps outlined inSetting up a load balancerwith Cloud Storagebuckets.
That example leads you through the creation of two resources:
http://IP_ADDRESS/never-fetch/three-cats.jpghttp://IP_ADDRESS/love-to-fetch/two-dogs.jpg
Where
/never-fetch/three-cats.jpgis stored in/cats, which isthe default service, and/love-to-fetch/two-dogs.jpgis stored in/dogs.At this point, the URL map looks like this:
gcloud compute url-maps describe http-lb
creationTimestamp:'2020-10-13T11:18:10.561-07:00'defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catsfingerprint:MKfYsObzqgw=hostRules:-hosts:-'*'pathMatcher:path-matcher-1id:'1420501688756228493'kind:compute#urlMapname:test-bucketpathMatchers:-defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catsname:path-matcher-1pathRules:-paths:-/love-to-fetch/*service:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogsselfLink:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/http-lb
Modifying the URL map
Important: The rewrite is prepended to the path as is. Full path rewrites arenot supported. External Application Load Balancers only implement path prefix rewrites. Forexample, youcan rewrite:host.name/path1/resource1 tohost.name/path2/resource1. Youcannot rewritehost.name/path1/resource1tohost.name/path1/resource2.In this example, you rewrite the/love-to-fetch/ URL so that users can reach thetwo-dogs.jpg image with this simplified URL:
http://IP_ADDRESS/two-dogs.jpg
To do this, modify the load balancer's URL map as follows:
Console
Edit your load balancer
- Go to the Load balancing page in the Google Cloud console.
Go to the Load balancing page - Click
http-lb. - ClickEdit.
- Keep the window open to continue.
Change the host and path rules
- In the left column of the screen, clickHost and path rules.
- SelectAdvanced host and path rule (URL redirect, URL rewrite).
- Click the row that contains the non-default path rule, in this case, therow that has an asterisk (
*) for all hosts. - Click the pencil icon for the
/love-to-fetch/* Route traffic to a single backend: dogsrow. - UnderPaths, delete
/love-to-fetch/*and add/*. - UnderAction, selectRoute traffic to a single backend.
- ClickAdd-on action (URL rewrite).
- LeaveHost rewrite blank.
- UnderPath prefix rewrite, enter
/love-to-fetch/. - UnderBackend, select
dogs. - ClickSave.
ClickDone.The new host and path rule looks as follows:
Paths Action Backend Any unmatched (default) Route traffic to a single backend cats /* Route traffic to a single backend dogs If everything looks correct, clickUpdate to update your HTTPload balancer.
gcloud
Create a YAML file
/tmp/http-lb.yaml, making sure to substitutePROJECT_ID with your project ID.When a user requests path
/*, the path gets rewritten in the backend tothe actual location of the content, which is/love-to-fetch/*.defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catshostRules:-hosts:-'*'pathMatcher:path-matcher-1name:http-lbpathMatchers:-defaultService:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/catsname:path-matcher-1pathRules:-paths:-/*routeAction:urlRewrite:pathPrefixRewrite:/love-to-fetch/service:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogstests:-description:Test routing to backend bucket, dogshost:example.compath:/love-to-fetch/testservice:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogsValidate the URL map.
gcloud compute url-maps validate --source /tmp/http-lb.yaml
If the tests pass and the command outputs a success message,save the changes to the URL map.
Update the URL map.
gcloud compute url-maps import http-lb \ --source /tmp/http-lb.yaml \ --global
Testing
Note the IPv4 address that was reserved:
gcloud compute addresses describe example-ip \ --format="get(address)" \ --global
After waiting several minutes for the changes to propagate, you can test thissetup.
On the command line, run the following curl command:
curl http://IP_ADDRESS/two-dogs.jpg
In a browser, openhttp://IP_ADDRESS/two-dogs.jpg.
What's next
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.