Mehran
Mehran

Reputation: 16821

Returning index.html for all the 404 requests on S3 instead of redirecting

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.

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.

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.

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

Upvotes: 5

Views: 5682

Answers (1)

Gabriel Bleu
Gabriel Bleu

Reputation: 10204

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

Upvotes: 8

Related Questions