URL should be show to file inside the bucket.

I have bucket named as staticcontent, structure as follows.

  • blogs
    • web-stories
      • a.html

Folder structure as follows(/blogs/web-stories/a.html)

In global external Application Load Balancer, I created backend bucket to serve data from staticcontent bucket, it works, so if i enter www.example.com/blogs/web-stories/a.html, it shows file(a.html) inside the web-stories folder,

Now, i want the url www.example.com/web-stories/a.html should be display the same file.

I want both urls, both should display same file.

Hi @rajasingh012 ,

Welcome to Google Cloud Community!

I understand that you desire to change www.example.com/blogs/web-stories/a.html to www.example.com/web-stories/a.html , both displaying the same content from the bucket. You can simply accomplish this by modifying your Url map.

As stated on the documentation, you should be able to create a YAML file that when a user requests path /, the path gets rewritten in the backend to the actual location of the content, which is in your case /blogs/.

Steps:

  • List your load balancer Url map on your console:

gcloud compute url-maps list

gcloud compute url-maps describe loadbalancername

Open terminal

  • Create a YAML file
  • Insert below script after changing all necessary details:
defaultService: https://www.googleapis.com/compute/v1/
PROJECT_ID/global/backendBuckets/<bucketname>
hostRules:
- hosts:
  - '*'
  pathMatcher: path-matcher-1
name: <loadbalancername>
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/<bucketname>
  name: path-matcher-1
  pathRules:
  - paths:
    - /*
    routeAction:
      urlRewrite:
        pathPrefixRewrite: /blogs/
    service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/<bucketname>

Then to apply on(CLI):

gcloud compute url-maps import <loadbalancername> \
   --source <yamlfilename> \
   --global

After applying the YAML and load balancer has been updated, you should be able to access the content when you enter www.example.com/web-stories/a.html*,* it may take a few minutes to load.

I would also recommend you to check on how to Set up a classic Application Load Balancer with Cloud Storage buckets. It is critical that you have your load balancer set up correctly before you proceed on modifying your Url map.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.