This example demonstrates rewriting the path specified in a request.
Before you begin
Read about URL rewrites.
The URL rewrite example assumes that you've already created your external HTTP(S) load balancer following the steps outlined in Setting up a load balancer with Cloud Storage buckets.
That example leads you through the creation of two resources:
/never-fetch/three-cats.jpgis stored in
/cats, which is the default service, and
/love-to-fetch/two-dogs.jpgis stored in
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/cats fingerprint: MKfYsObzqgw= hostRules: - hosts: - '*' pathMatcher: path-matcher-1 id: '1420501688756228493' kind: compute#urlMap name: test-bucket pathMatchers: - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats name: path-matcher-1 pathRules: - paths: - /love-to-fetch/* service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs selfLink: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/http-lb
Modifying the URL map
In this example, you rewrite the
/love-to-fetch/ URL so that users can reach the
two-dogs.jpg image with this simplified URL:
To do this, modify the load balancer's URL map as follows:
Edit your load balancer
- Go to the Load balancing page in the Google Cloud Console.
Go to the Load balancing page
- Click Edit .
- Keep the window open to continue.
Change the host and path rules
- In the left column of the screen, click Host and path rules.
- Select Advanced host and path rule (URL redirect, URL rewrite).
- Click the row that contains the non-default path rule, in this case, the
row that has an asterisk (
*) for all hosts.
- Click the pencil icon
/love-to-fetch/* Route traffic to a single backend: dogsrow.
- Under Paths, delete
- Under Action, select Route traffic to a single backend.
- Click Add-on action (URL rewrite).
- Leave Host rewrite blank.
- Under Path prefix rewrite, enter
- Under Backend, select
- Click Save.
Click Done. 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, click Update to update your HTTP load balancer.
Create a YAML file
/tmp/http-lb.yaml, making sure to substitute PROJECT_ID with your project ID.
When a user requests path
/*, the path gets rewritten in the backend to the actual location of the content, which is
defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats hostRules: - hosts: - '*' pathMatcher: path-matcher-1 name: http-lb pathMatchers: - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats name: path-matcher-1 pathRules: - paths: - /* routeAction: urlRewrite: pathPrefixRewrite: /love-to-fetch/ service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs
Update the URL map.
gcloud compute url-maps import http-lb \ --source /tmp/http-lb.yaml \ --global
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 this setup.
On the command line, run the following curl command:
In a browser, open