18
votes

I'm trying to serve a static website using GCP and Google Cloud Storage. I have done everything specified here https://cloud.google.com/storage/docs/hosting-static-website

Say I have the following file structure:

- index.html
- folder
    - index.html
    - another.html

I enter www.example.com/folder.

It redirects to www.example.com/folder/index.html => index.html is undesired

But if I enter www.example.com/folder/, it doesn't redirect and serves correctly the index.html page transparently.

How do I prevent the first behavior? Or automatically add a trailing slash?

Thanks!

5
Did you manage to find a solution to this? The answer citing setting the MainPageSuffix doesn't solve the problem.Will Viles
facing this issue as well. this is annoying. was thinking about using a small javascript snippet to "spank" the .../index.html to .../Jossef Harush
You have to describe routing rules in app.yaml. How does your app.yaml look like?V.Tran
@V.Tran they're describing Google Cloud Storage static website hosting - it's not related to app engine.Louis Sayers
For anyone experiencing this issue, you might want to follow issuetracker.google.com/issues/151713401Louis Sayers

5 Answers

1
votes

Firebase Hosting might be easier than using a bucket, because one can define rewrite rules there.

0
votes

As per https://cloud.google.com/storage/docs/hosting-static-website this behaviour is intended. As to whether it's desirable, that's a bigger issue.

For example, say you set the MainPageSuffix of your static website to index.html. Additionally, say you have no file named directory in your bucket www.example.com. In this situation, if a user requests the URL http://www.example.com/directory, Cloud Storage attempts to serve the file www.example.com/directory/index.html. If that file also doesn't exist, Cloud Storage returns an error page.

The MainPageSuffix also controls the file served when users request the top level site. Continuing the above example, if a user requests http://www.example.com, Cloud Storage attempts to serve the file www.example.com/index.html

Unfortunately I do not know of a way to automatically add the trailing slash. Perhaps you could add a message on index.html to the tune of "Did you mean to go to http://example.com/thisfolder/ instead of http://example.com/thisfolder/index.html", but this is something of a kludgy workaround.

-2
votes

You should be able to set the MainPageSuffix to the index.html accordingly. For more information, check the additional notes available in public documentation link. Reference example can be found in the same documentation.

Syntax:

gsutil web set [-m main_page_suffix] bucket_url
-2
votes

If you follow the tutorial above, then you must have a app.yaml file to describe your routing rules. Here is an answer for that:

Angular 7 Routing in Google Cloud App Engine not working

-3
votes

Check this out

https://cloud.google.com/storage/docs/gsutil/commands/web#description

The main_page_suffix applies to each subdirectory of the bucket. For example, with the main_page_suffix configured to be index.html, a GET request for http://www.example.com would retrieve http://www.example.com/index.html, and a GET request for http://www.example.com/photos would retrieve http://www.example.com/photos/index.html.