S3 static website hosting: redirect all paths to index.html
P粉127901279
P粉127901279 2023-10-15 23:02:52
0
1
582

I'm using S3 to host a JavaScript application that will use HTML5 PushStates. The problem is that if the user bookmarks any URL, it won't resolve to anything. What I need is to be able to accept all url requests and serve the root index.html in my S3 bucket, rather than just do a full redirect. My JavaScript application can then parse the URL and serve the correct page.

Is there a way to tell S3 to serve index.html for all URL requests instead of redirecting? This is similar to setting up apache to handle all incoming requests by serving a single index.html, as shown in this example: https://stackoverflow.com/a/10647521/1762614. I'd really like to avoid running a webserver just to handle these routes. It's very tempting to do everything from S3.

P粉127901279
P粉127901279

reply all (1)
P粉231112437

With the help of CloudFront, this problem can be easily solved without URL hack.

  • Create S3 bucket, for example: react
  • Create a CloudFront distribution with the following settings:
    • Default root object: index.html
    • Source domain name: S3 bucket domain, for example: react.s3.amazonaws.com
  • Go to theError Pagetab and clickCreate Custom Error Response:
    • HTTP Error Codes: 403: Forbidden (404: Not Found, if S3 static website)
    • Custom error response: Yes
    • Response page path:/index.html
    • HTTP response code: 200: OK
    • ClickCreate
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!