How to Redirect example.com to www.example.com Using AWS S3, CloudFront & Route 53

KUNALKUNAL
2 min read

Introduction

When hosting a static website on AWS, ensuring your domain redirects correctly from example.com to www.example.com is essential for SEO, brand consistency, and user experience.

In this guide, we’ll walk through the exact setup of S3 buckets, CloudFront distributions, and Route 53 DNS records to seamlessly redirect example.com to www.example.com. Whether you're hosting a static site or building a personal portfolio, this method ensures both domains are properly served.

Architecture Overview

Step 1: Host Your Main Site on www.example.com

  1. Create an S3 Bucket:

    • Name it www.example.com (must match your domain).

    • Go to Properties → Static Website Hosting.

    • Choose “Host a static website”.

    • Set index.html as the index document.

    • Upload your website files (e.g., index.html with demo text like “Hello from www!”).


Step 2: Set Up CloudFront for www.example.com

  1. Create a CloudFront Distribution:

    • Origin domain: Select your S3 bucket www.example.com.

    • Viewer Protocol Policy: Redirect HTTP to HTTPS.

    • Alternate Domain Name (CNAME): www.example.com.

    • (Optional) Add SSL via ACM for HTTPS.

  2. Deploy the distribution.

    • Copy the CloudFront domain (e.g., d1234.cloudfront.net).

Step 3: Point DNS to CloudFront

  1. Go to Route 53 → Hosted Zone → www.example.com.

  2. Add a new A Record (Alias):

    • Name: www

    • Type: A – IPv4 address

    • Alias: Yes

    • Target: CloudFront distribution

Now, your site is live at www.example.com


Step 4: Redirect example.com to www.example.com

Now let’s handle the redirection part.

  1. Create a Second S3 Bucket:

    • Name it example.com (no www).

    • Go to Properties → Static Website Hosting.

    • Choose “Redirect requests for an object”.

    • Target bucket or domain: www.example.com.

    • Protocol: https.

This tells S3 to redirect any requests to example.comhttps://www.example.com.


Step 5: Set Up CloudFront for example.com Redirect

  1. Create another CloudFront Distribution:

    • Origin domain: Choose the new example.com S3 redirect bucket.

    • Alternate Domain Name (CNAME): example.com.

    • Viewer Protocol Policy: Redirect HTTP to HTTPS.

    • Behavior: Set to forward viewer request.

  2. Deploy the distribution.

    • Copy the new CloudFront domain (e.g., d5678.cloudfront.net).

Step 6: Point DNS for example.com

  1. Go to Route 53 → Hosted Zone → example.com.

  2. Add a new A Record (Alias):

    • Name: (leave blank for root domain)

    • Type: A – IPv4 address

    • Alias: Yes

    • Target: Redirect CloudFront distribution

Now, your setup will work like this:

  • Visiting www.example.com shows your content.

  • Visiting example.com redirects to www.example.com.

0
Subscribe to my newsletter

Read articles from KUNAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KUNAL
KUNAL