为S3上的所有404请求返回index.html,而不是重定向 [英] Returning index.html for all the 404 requests on S3 instead of redirecting

查看:131
本文介绍了为S3上的所有404请求返回index.html,而不是重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在单页应用程序中使用路由机制,其中所有内容都保留在index.html中,而Javascript通过在浏览器中显示正确的内容而不是实际发送任何请求来管理URL寻址.对此的唯一要求是,在服务器端,所有指向不存在的条目的URL都应返回index.html页.我正在尝试使用S3.

In single page applications, usually, there's routing mechanism used where all the content is kept in index.html and Javascript manages URL addressing by showing the right content in the browser instead of actually sending any request. The only requirement for this is that on the server side, all the URLs pointing to non-existing entries should return the index.html page. I'm trying to do this with S3.

因此,长话短说,我已经设置了一个S3存储桶来保存网站的静态内容,甚至通过在index.html存储桶的>重定向规则" 框:

So, long story short, I've already set up an S3 bucket holding my website's static content and I even managed to redirect all the URLs pointing to non-existing files to index.html by entering the following rule in Redirection rules box of the bucket:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith></ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
</RoutingRules>

,还授予公众列出该存储桶中项目的权限.因此,一切似乎都很好,但事实并非如此.

And also granting the public the permission of listing items of that bucket. So everything seems fine but they are not.

使用给定规则,我要求AWS将所有请求重定向到/,而我需要的是AWS应该为任何不匹配现有URL的URL返回(不重定向)index.html的内容文件.

Using the given rule, I'm asking AWS to redirect all my requests to / while what I need is that AWS should return (without redirection) the content of index.html for any URL that is not matching an existing file.

这可能吗?有人知道如何为指向不存在文件的所有URL返回index.html吗?

Is this possible? Does anyone know how to return index.html for all the URLs pointing to non-existing files?

推荐答案

您可以将所有4XX错误重定向到index.html,方法是将其放置在静态网站托管下的错误文档配置中.

You can redirect all 4XX errors to index.html by putting it in the error document config under Static website hosting.

ref: https://serverless-stack.com/chapters/create-an-s3-bucket.html#enable-static-web-hosting

这篇关于为S3上的所有404请求返回index.html,而不是重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆