Search code examples
amazon-web-servicesamazon-s3amazon-cloudfront

Serve index.html without a redirect on 404 keeping url


I'm using an SPA and my local dev environment serves the index.html file for any 404s. This keeps the URL intact when loading the app. My apps routing picks up the URL and displays the correct page.

I currently have 404 redirects in s3 to index.html

Current behavior

  • Navigate to www.example.com/clients
  • s3 detects 404
  • redirect to www.example.com/index.html
  • SPA sees the endpoint is /index.html and cannot find page

Desired behavior

  • Navigate to www.example.com/clients
  • detect 404
  • Serve index.html no redirect
  • SPA sees the endpoint is /clients and displays clients page

I'm hoping I can achieve this without needing to run a server


Solution

  • If you configure index.html for both Index Document and Error document with no redirects configured, then:

    • Request for www.example.com shows www.example.com/index.html with 200 status code, client sees www.example.com.
    • Request for www.example.com/sub1 (found) shows www.example.com/sub1/index.html with 200 status code, client sees www.example.com/sub1 with no redirect.
    • Request for www.example.com/sub1 (not found) shows www.example.com/index.html with 404 status code, client sees www.example.com/sub1 with no redirect.

    There is no way to change this behaviour. If you want it different, then you'll need to use your own server.