Redirect to index.html for S3 subfolder

g3blv picture g3blv · Mar 3, 2018 · Viewed 12.2k times · Source

I have a domain example.com. I have a S3 bucket named example.com setup with an index.html file that works. Now I like to create two subfolders called old and new, each containing a separate version of a single page application. Requesting https://example.com/old (I like to omit the index.html when entering the request in address bar for browser) would open the index.html file in the old subfolder and requesting https://example.com/new would open the index.html. What is the best way of doing these redirects? Should I set something up in Route 53 example.com/old -> example.com/old/index.html or is there a better way of doing it?

Answer

Marc Guiselin picture Marc Guiselin · Jul 7, 2018

No need for a lambda function adding expense and complexity to your project.

The following answer is quoted from https://stevepapa.com/

https://stevepapa.com/my-great-new-post/ would be expected to work the same way as: https://stevepapa.com/my-great-new-post/index.html

There’s a clever little way to get these flowing through to the Cloudfront distribution, and it involves changing the source origin from the one that Cloudfront presents to you by default.

When selecting the origin source Cloudfront will show you a list of S3 buckets. editing origin

Instead of setting the source from the bucket shown in the dropdown list, you’ll need to grab the static web hosting endpoint for that resource from its S3 settings page and pop it in manually. where the static hosting endpoint url is

Using the static source for the Cloudfront distribution origin means any request to that distribution will be using the S3’s root object lookup, and your 404 responses should disappear as the references flow through.

Important: After doing this, clear your browser cache and devalidate the items in your cloudfront distribution. Otherwise, the changes you made won't go live immediately.