Setting Up Domain Redirects with AWS: From www to Naked Domain

SHUBHAM MEHRASHUBHAM MEHRA
5 min read

Introduction

In web hosting, a common requirement is to redirect traffic from the www subdomain (e.g., www.example.com) to the apex or "naked" domain (e.g., example.com). This ensures users reach your site regardless of which URL version they enter, providing a consistent user experience and avoiding duplicate content issues for SEO.

This guide outlines a robust approach to implement domain redirects using AWS services, specifically focusing on redirecting both HTTP and HTTPS traffic from www.domain.com to domain.com.

Architecture Overview

The solution leverages several AWS services working together:

  1. Amazon S3 - For hosting redirect configurations

  2. Amazon CloudFront - For content delivery and HTTPS termination

  3. AWS Route 53 - For DNS management

Prerequisites

  • An AWS account with appropriate permissions

  • A registered domain name

  • Basic familiarity with AWS Management Console

Implementation Steps

Step 1: Create S3 Buckets

We'll create an S3 bucket to handle the redirect configuration:

  1. Sign in to the AWS Management Console and open the S3 console at https://console.aws.amazon.com/s3/

  2. Click Create bucket

  3. Enter www.xyz.io as the bucket name (replace with your actual domain)

  4. Select your preferred region

  5. Accept default settings and click Create bucket

Step 2: Configure the www Bucket for Redirection

  1. In the S3 console, select the www.xyz.io bucket

  2. Navigate to the Properties tab

  3. Scroll down to Static website hosting and click Edit

  4. Select Redirect requests for an object

  5. In the Host name field, enter your naked domain: xyz.io

  6. Set Protocol to https

  7. Click Save changes

Note the website endpoint URL that appears after enabling static website hosting. It will look something like: http://www.xyz.io.s3-website-us-east-1.amazonaws.com

Step 3: Set Up CloudFront Distribution

To enable HTTPS support for the redirect, we'll create a CloudFront distribution:

  1. Open the CloudFront console at https://console.aws.amazon.com/cloudfront/

  2. Click Create Distribution

  3. For Origin Domain, enter the S3 website endpoint from Step 2

  4. For Origin Protocol Policy, select HTTP Only

  5. Under Default Cache behaviour Settings:

    • For Viewer Protocol Policy, choose Redirect HTTP to HTTPS

    • Keep other settings at their defaults

  6. Under Alternate Domain Names (CNAMEs), enter www.xyz.io

  7. Under Custom SSL Certificate, select or upload your SSL certificate for the domain

    • You can request a free certificate through AWS Certificate Manager
  8. Set other settings as needed and click Create Distribution

Wait for the distribution to deploy (status will change from "In Progress" to "Deployed")

Step 4: Configure DNS in Route 53

  1. Open the Route 53 console at https://console.aws.amazon.com/route53/

  2. Navigate to Hosted zones and select your domain's hosted zone

  3. Click Create record

  4. For the record with www subdomain:

    • Enter www in the Name field

    • Select A - Routes traffic to an IPv4 address and some AWS resources as record type

    • Toggle on Alias

    • For Route traffic to, select Alias to CloudFront distribution

    • Select the CloudFront distribution you created in Step 3

    • Click Create records

Step 5: Verify the Configuration

After the DNS changes propagate (which can take up to 48 hours, but usually much less), test the redirects:

  • http://www.xyz.io → should redirect to → https://xyz.io

  • https://www.xyz.io → should redirect to → https://xyz.io

  • http://xyz.io → should redirect to → https://xyz.io

  • https://xyz.io → should load directly

Verification Best Practices

To ensure your configuration is working correctly:

  1. Use Incognito Mode: Test all redirects in an incognito/private browsing window to avoid cache-related issues that might mask problems.

  2. Verify DNS Resolution:

     bash# Verify that www points to CloudFront
     nslookup www.xyz.io
     dig www.xyz.io
    

    The results should show that your www subdomain resolves to CloudFront's domain (e.g., d1234abcdef.cloudfront.net).

  3. Confirm from Multiple Devices/Networks: Different ISPs may have different caching behaviors.

How It Works

  1. When a user visits www.xyz.io:

    • If using HTTP, CloudFront first redirects to HTTPS

    • The request reaches the S3 bucket configured for website hosting

    • S3 returns a 301 redirect to the naked domain xyz.io

    • The browser follows the redirect to https://xyz.io

  2. For the naked domain xyz.io:

    • If using HTTP, it redirects to HTTPS

    • The HTTPS version loads directly from your primary hosting

Benefits of This Approach

  • SEO Friendly: Uses proper 301 redirects

  • Secure: All traffic is served over HTTPS

  • Scalable: CloudFront's global edge network ensures fast redirects

  • Maintenance-Free: Once set up, the solution requires no ongoing maintenance

  • Cost-Effective: S3 website hosting costs are minimal for redirects

Potential Issues and Troubleshooting

DNS Propagation Delays

DNS changes can take time to propagate globally. If redirects aren't working, wait a few hours and try again.

Certificate Issues

Ensure your SSL certificate covers both the naked domain and the www subdomain. AWS Certificate Manager can provision certificates that include both.

S3 Bucket Naming

Remember that S3 bucket names must exactly match your domain name when used for website hosting.

Browser caching can sometimes mask whether redirects are working correctly. To verify your setup:

  1. Use Incognito/Private Browsing Mode: This bypasses your browser's cache, showing the current redirect behaviour without cached data interfering.

  2. Verify DNS Resolution with Command Line Tools:

     bash# Check DNS resolution for www subdomain
     nslookup www.xyz.io
    
     # Get more detailed DNS information
     dig www.xyz.io
    

    The results should show that your www subdomain points to CloudFront's servers (the domain will look like d1234abcd.cloudfront.net).

  3. Verify CloudFront Distribution:

     bash# Compare with direct CloudFront domain lookup
     nslookup [your-cloudfront-distribution-id].cloudfront.net
    

    The IP addresses returned for your www subdomain and the CloudFront distribution should match, confirming your DNS is correctly pointing to CloudFront.

  4. Clear DNS Cache on Your Computer:

    • On Windows: Run ipconfig /flushdns in Command Prompt

    • On macOS: Run sudo killall -HUP mDNSResponder in Terminal

    • On Linux: Varies by distribution, often sudo systemd-resolve --flush-caches

  5. Test from Different Networks: Sometimes ISP-level caching can affect testing results.

Conclusion

Setting up proper domain redirects is a crucial step in establishing a professional web presence. The approach outlined in this guide uses AWS services to create a robust, scalable solution that ensures visitors to your site have a consistent experience regardless of which URL they use to access it.

By leveraging S3 for redirect configuration, CloudFront for content delivery and HTTPS termination, and Route 53 for DNS management, you create a solution that is both reliable and requires minimal maintenance.


Have you implemented domain redirects using a different approach? Share your experience in the comments below!

0
Subscribe to my newsletter

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

Written by

SHUBHAM MEHRA
SHUBHAM MEHRA

Meticulous web developer with over 4 years of experience and passion for metrics and beating former "best-yets."