【问题标题】:Redirect all AWS S3 http requests to index.html for AngularJS HTML5Mode将所有 AWS S3 http 请求重定向到 AngularJS HTML5Mode 的 index.html
【发布时间】:2017-10-14 14:04:51
【问题描述】:

如何将所有对我的静态 AWS S3 网站的请求重定向到 index.html,以便我可以使用 AngularJS 的 HTML5 模式?

我最近了解到(令我欣喜若狂)可以通过使用HTML5 Mode 在 URL 中不使用 # 的情况下使用 AngularJS。但是,我从this answer 知道,这需要在服务器上进行一些设置,因为所有请求都必须重定向到正确的 html 文件(在本例中为 index.html)才能正常工作。

我为我的网站使用 AWS S3 的静态网站托管。我尝试将此添加到我的重定向规则中:

<RoutingRules>
     <RoutingRule>
         <Redirect>
             <ReplaceKeyWith>/</ReplaceKeyWith>
         </Redirect>
     </RoutingRule>
</RoutingRules>

<RoutingRules>
     <RoutingRule>
         <Redirect>
             <ReplaceKeyWith>index.html</ReplaceKeyWith>
         </Redirect>
     </RoutingRule>
</RoutingRules>

但我遇到了重定向过多的问题。

有没有办法通过静态网站托管在 AWS S3 中进行必要的重定向?

【问题讨论】:

    标签: angularjs amazon-web-services amazon-s3 html5mode


    【解决方案1】:

    您可以将 AWS CloudFront 用于您的用例。在 CloudFront 后面设置 S3 存储桶,并将 index.html 添加为默认路由。

    如果页面以角度路径(例如 /home)刷新,AWS CloudFront 将在 S3 中搜索 /home.html 文件并返回 404:未找到响应。但是有一个解决方法,您可以为 404: Not Found HTTP 错误代码设置自定义错误响应以指向 /index.html 响应页面路径。

    更多详情请参考博文Using AWS CloudFront to serve an SPA hosted on S3

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2014-06-30
      • 2017-04-30
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多