Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCP - Static hosting - Redirects to /index.html

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!

like image 329
Danetag Avatar asked Dec 20 '17 16:12

Danetag


2 Answers

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.

like image 150
J. Olsson Avatar answered Oct 22 '22 11:10

J. Olsson


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

like image 30
Martin Zeitler Avatar answered Oct 22 '22 09:10

Martin Zeitler