GCP - 静态托管 - 重定向到 /index.html

问题描述 投票:0回答:6

我正在尝试使用 GCP 和 Google Cloud Storage 提供静态网站。我已完成此处指定的所有操作https://cloud.google.com/storage/docs/hosting-static-website

假设我有以下文件结构:

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

我输入

www.example.com/folder

它重定向到

www.example.com/folder/index.html
=> index.html 是不需要的

但是如果我输入

www.example.com/folder/
,它不会重定向并透明地正确提供 index.html 页面。

如何预防第一种行为?或者自动添加尾部斜杠?

谢谢!

redirect google-cloud-platform google-cloud-storage
6个回答
2
投票

根据https://cloud.google.com/storage/docs/hosting-static-website,此行为是有意的。至于合不合意,那就是一个更大的问题了。

例如,假设您将静态网站的 MainPageSuffix 设置为 索引.html。另外,假设您的目录中没有名为目录的文件 存储桶 www.example.com。在这种情况下,如果用户请求 URL http://www.example.com/directory,Cloud Storage 尝试服务于 文件 www.example.com/directory/index.html。如果该文件也没有 存在,Cloud Storage 返回错误页面。

MainPageSuffix 还控制用户请求时提供的文件 顶级网站。继续上面的例子,如果用户请求 http://www.example.com,Cloud Storage 尝试提供文件 www.example.com/index.html

不幸的是,我不知道如何自动添加尾部斜杠。也许您可以在 index.html 上添加一条消息,内容是“您是否打算访问 http://example.com/thisfolder/ 而不是 http://example.com/thisfolder/index.html ”,但这是一个笨拙的解决方法。


1
投票

Firebase 托管可能比使用存储桶更容易,因为可以在那里定义重写规则


0
投票

如果您只有几个页面并且站点布局不经常更改,您可以使用全局外部应用程序负载均衡器的流量管理重写请求的url以透明地在'目录的位置(即没有斜杠或index.html):

defaultService: https://www.googleapis.com/compute/v1/projects/{YOUR_PROJECT_ID}/global/backendBuckets/{YOUR_BUCKET_BACKEND_ID}
hostRules:
- hosts:
  - www.example.com
  pathMatcher: static
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/{YOUR_PROJECT_ID}/global/backendBuckets/{YOUR_BUCKET_BACKEND_ID}
  name: static
  routeRules:
  - matchRules:
    - prefixMatch: /folder
    priority: 10
    routeAction:
      urlRewrite:
        pathPrefixRewrite: /folder/index.html
    service: https://www.googleapis.com/compute/v1/projects/{YOUR_PROJECT_ID}/global/backendBuckets/{YOUR_BUCKET_BACKEND_ID}

-1
投票

您应该能够相应地将 MainPageSuffix 设置为index.html。有关更多信息,请查看公共文档链接中提供的附加说明。参考示例可以在同一文档中找到。

语法:

gsutil web set [-m main_page_suffix] bucket_url

-2
投票

如果您按照上面的教程进行操作,那么您必须有一个 app.yaml 文件来描述您的路由规则。这是一个答案:

Google Cloud App Engine 中的 Angular 7 路由不起作用


-2
投票

看看这个

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

main_page_suffix 适用于存储桶的每个子目录。例如,将 main_page_suffix 配置为 index.html,对 http://www.example.com 的 GET 请求将检索 http://www.example.com/index.html,并且 GET 请求对于 http://www.example.com/photos 将检索 http://www.example.com/photos/index.html

© www.soinside.com 2019 - 2024. All rights reserved.