キャンペーン終了したりして、ドメイン丸ごと別サイトにリダイレクトしたい場合など。
要は、S3のBucketに何もファイルがないと、常にアクセスが404エラーになるので、404エラーのリダイレクト先として別サイトを指定すると、結果として全てのアクセスがリダイレクトされるということです。
2. 誰でもアクセスできるようにbucket policyを設定する
BucketのPropertiesを開いて、Permissions → Edit bucket policyで設定できる。以下のようなbucket policyで、誰でもs3:GetObjectできるように。
"Resource": "arn:aws:s3:::example.jp/*" の部分は、適宜対象ドメイン名で置換する。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadForGetBucketObjects", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::example.jp/*" ← example.jpは対象ドメイン名で置き換える } ] }
3. Static Website Hostingを設定する
BucketのPropertiesを開いて、Static Website Hostingを設定する。
<RoutingRules> <RoutingRule> <Condition> <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> </Condition> <Redirect> <HostName>www.example.co.jp</HostName> ← リダイレクト先のホスト名 <ReplaceKeyWith/> <HttpRedirectCode>301</HttpRedirectCode> </Redirect> </RoutingRule> </RoutingRules>