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

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:
Amazon S3 - For hosting redirect configurations
Amazon CloudFront - For content delivery and HTTPS termination
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:
Sign in to the AWS Management Console and open the S3 console at https://console.aws.amazon.com/s3/
Click Create bucket
Enter
www.xyz.io
as the bucket name (replace with your actual domain)Select your preferred region
Accept default settings and click Create bucket
Step 2: Configure the www Bucket for Redirection
In the S3 console, select the
www.xyz.io
bucketNavigate to the Properties tab
Scroll down to Static website hosting and click Edit
Select Redirect requests for an object
In the Host name field, enter your naked domain:
xyz.io
Set Protocol to
https
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:
Open the CloudFront console at https://console.aws.amazon.com/cloudfront/
Click Create Distribution
For Origin Domain, enter the S3 website endpoint from Step 2
For Origin Protocol Policy, select
HTTP Only
Under Default Cache behaviour Settings:
For Viewer Protocol Policy, choose
Redirect HTTP to HTTPS
Keep other settings at their defaults
Under Alternate Domain Names (CNAMEs), enter
www.xyz.io
Under Custom SSL Certificate, select or upload your SSL certificate for the domain
- You can request a free certificate through AWS Certificate Manager
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
Open the Route 53 console at https://console.aws.amazon.com/route53/
Navigate to Hosted zones and select your domain's hosted zone
Click Create record
For the record with
www
subdomain:Enter
www
in the Name fieldSelect 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:
Use Incognito Mode: Test all redirects in an incognito/private browsing window to avoid cache-related issues that might mask problems.
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
).Confirm from Multiple Devices/Networks: Different ISPs may have different caching behaviors.
How It Works
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
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.
Cache-Related Issues
Browser caching can sometimes mask whether redirects are working correctly. To verify your setup:
Use Incognito/Private Browsing Mode: This bypasses your browser's cache, showing the current redirect behaviour without cached data interfering.
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
).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.
Clear DNS Cache on Your Computer:
On Windows: Run
ipconfig /flushdns
in Command PromptOn macOS: Run
sudo killall -HUP mDNSResponder
in TerminalOn Linux: Varies by distribution, often
sudo systemd-resolve --flush-caches
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!
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."