In my last post, I set up a simple S3 static website for the domain www.hiredatagram.com. The only problem is that the naked domain http://hiredatagram.com doesn’t resolve. In this post, I will show you how to redirect an S3 bucket (naked domain) to the S3 bucket that host the static website (www). We will not be using Godaddy for DNS hosting as they don’t support CNAME flattening. Cloudflare, however, does support CNAME flattening.
- Step 1: Setup S3 Bucket To Redirect To the WWW version of the site.
- Step 2: Setup a new CNAME (DNS) Record inside Cloudflare
- Step 3: Testing Redirect with curl
Step 1: Setup S3 Bucket To Redirect To the WWW version of the site.
The first thing we need to do is create a new S3 bucket and name it as the domain name we want to resolve.
- First login into AWS and choose S3 as the service.
- Next, click **Create Bucket
- Type the name of the domain without the “WWW” for your bucket name.
- Just accept all the defaults on the next 2 screens and click Create bucket to finish.
- Now, click on your newly created bucket name and click on properties.
- Click on the box that says Static Website Hosting
- When the options appear choose Redirect requests.
- In the Target bucket or domain type your www version of your website and click save. In my case, I will type www.hiredatagram.com
- Now click back on the Static Website Hosting box and copy the Endpoint address. You will need this to create your new CNAME DNS record.
Step 2: Setup DNS
In my first post, my test domain’s DNS was hosted at Godaddy. In this post, my DNS is now moved to Cloudflare because they support apex or cname flattening. Moving your DNS to another provider is beyond the scope of this article, but please do your research before attempting to move your DNS.
You need to add your naked domain, in my case hiredatagram.com to your dns and point it endpoint address on your bucket.
The type will be a CNAME and the Name value will be @ symbol. The Value will be my endpoint address of my new bucket: hiredatagram.com.s3-website.us-east-2.amazonaws.com
**Make sure you don’t put the **HTTP:// or trailing slashes in the Value.
If you receive a warning that is normal .
Step 3: Testing Redirect
I will use curl to test the redirect. If you don’t have curl installed you can use an online redirect tester site.