Redirect a subdomain to your S3 static website

Redirect a subdomain to your S3 static website

In this article we shown how to deploy a static website with S3. Here you will learn how to direct a subdomain to another bucket with a different domain. This is especially useful to be able to access the S3 bucket by using the www subdomain. In fact if you create a S3 website, e.g. test-static-site.com, the subdomain www.test-static-site.com will not direct you to the S3 bucket automatically. In order to do it, follow these steps:

1. Create a S3 bucket

  • Go to S3
  • Create a new bucket and set the name to be equal to the domain that you want to be redirected (e.g. www.test-static-site.com)

2. Make bucket public

Modifiy the permissions to allow everyone to access your bucket, the procedure is described here. The final permissions should be the same as the bucket that you want to redirect to.

3. Enable S3 website hosting

  • Go to Properties tab
  • Scroll down to the section Static website hosting and click on Edit
  • Enable Static website hosting from here and choose Redirect requests for an object
  • Add the domain you want to redirect this endpoint to and choose protocol http

Before going to the next step, check if the S3 endpoint that you just created is working correctly and redirects you to the bucket that you configured

4. Create a Route53 record

  • Go to Route53
  • Enter the hosted zone that matches your domain and create a new record
  • Set record type to CNAME and specify the subdomain that mathches the new S3 bucket name
  • Set the record value as the S3 endpoint and save

After the record is propagated (approximately 60 seconds) your S3 website will be accessible by the subdomain you configured

Roberto
Roberto Founder and Author of Codevup