Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Created February 28, 2017 02:57
Show Gist options
  • Save YanhaoYang/15fe2197c7abdb3542b082e4eb7eae29 to your computer and use it in GitHub Desktop.
Save YanhaoYang/15fe2197c7abdb3542b082e4eb7eae29 to your computer and use it in GitHub Desktop.
AWS S3 Static Website Hosting with redirection

Redirection Rules

Suppose there are two buckets: one-bucket, another-bucket.

Show content in another bucket when the file is missing in one bucket:

  <RoutingRules>
    <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals >
    </Condition>
    <Redirect>
      <HostName>another-bucket.s3.amazonaws.com</HostName>
    </Redirect>
    </RoutingRule>
  </RoutingRules>

Test it

curl -v http://one-bucket.s3-website-ap-southeast-1.amazonaws.com/some-image.jpg
*   Trying 52.219.40.46...
* Connected to one-bucket.s3-website-ap-southeast-1.amazonaws.com (52.219.40.46) port 80 (#0)
> GET /x7KJ/0.jpg HTTP/1.1
> Host: one-bucket.s3-website-ap-southeast-1.amazonaws.com
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< x-amz-id-2: cox0/fIZ91JZTG9Sbrgt6Yx6hlJ9TqMR4UL5F18p3JSgGneS+1CcDgCTVD5A45l7YIDyPavpesU=
< x-amz-request-id: DB156A54006C616E
< Date: Tue, 28 Feb 2017 02:55:27 GMT
< Location: http://another-bucket.s3.amazonaws.com/some-image.jpg
< Content-Length: 0
< Server: AmazonS3
< Connection: close
< 
* Closing connection 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment