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

S3 How to route all the requests to the same resource?


Im thinking of hosting a website on S3. The content of the webpages will be stored elsewhere on our web service. Therefore S3 would only be delivering a HTML page, CSS and crucially JS. Then AJAX requests would retrieve data and display it on the webpage.

Examples of what im looking for

https://www.example.com/something/another/thing
routes to s3/mybucket/index.html

https://www.example.com/something
routes to s3/mybucket/index.html

https://example.com
routes to s3/mybucket/index.html

Therefore the question is, how can I route all the requests to the same resource?


Solution

  • Finally managed to figure it out. Using ReplaceKeyPrefixWith does not actually redirect the page it just routes it to the desired destination. Using KeyPrefixEquals set to "/" means all requests will be re-routed, except the index.html.

    <RoutingRules>
        <RoutingRule>
            <Condition>
                <KeyPrefixEquals>/</KeyPrefixEquals>
            </Condition>
            <Redirect>
                <ReplaceKeyPrefixWith>index.html</ReplaceKeyPrefixWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>