Subfolder redirect issue with static website hosting using S3, CloudFront and Origin Path

后端 未结 1 552
广开言路
广开言路 2021-02-14 22:41

I\'m having some difficulties setting up static website hosting using Amazon S3 and Cloudfront.

We have many websites that we would like to serve as static websites usin

相关标签:
1条回答
  • 2021-02-14 23:07

    I ended up solving it by using routing rules for the S3 bucket

    https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html

    the problem is the redirect caused by omitting a trailing slash results in the Orgigin Path being appended to the full S3 bucket path ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that fails because the path is invalid)

    The below routing rule solves this by triggering on the faulty path pattern prefix and redirecting back to the Cloudfront distribution with the prefix stripped from the request, i.e ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that redirects to "example.cloudfront.net/about/")

    The downside is that you need to remember to modify the routing rules when adding new distributions

    <RoutingRules>
        <RoutingRule>
            <Condition>
                <KeyPrefixEquals>websites/website1/websites/website1/</KeyPrefixEquals>
            </Condition>
            <Redirect>
                <HostName>example.cloudfront.net</HostName>
                <ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>
    
    0 讨论(0)
提交回复
热议问题