AWS Topics
- AWS Intro
- AWS Launch EC2 Instance
- AWS Elastic IP
- AWS Target Group
- AWS Application Load Balancer
- AWS Route53
- AWS Amazon Certificate Manager
- AWS Redirect HTTP to HTTPs
- AWS Redirect WWW to Non WWW
- AWS Cloudfront
- AWS Download S3 to Local
- AWS Install Node.js & NPM in EC2
- AWS EC2 Connect PuTTY
- AWS EC2 Connect FileZilla
- AWS All-in-One WP Migration
- AWS Deploy Node.js
- AWS Deploy Express.js
- AWS Deploy ReactJS
- AWS Deploy NextJS
- AWS Deploy NextJS with Express.js
- AWS Deploy Wordpress
WWW to Non-WWW Redirection
Photo Credit to CodeToFun
🙋 Introduction
Redirecting from www to non-www (e.g., from www.example.com to example.com) is a common practice to maintain a consistent domain structure.
If you have an Application Load Balancer (ALB) serving your domain through an AWS Route 53 hosted zone, this tutorial will guide you through the process of setting up the redirection.
📚 Prerequisites
Before you begin, make sure you have the following:
- An AWS account with access to the AWS Management Console.
- An Application Load Balancer (ALB) set up and associated with your domain through Route 53.
✏️ Create a New Listener Rule HTTP:80
Follow the steps below to create an HTTP listener rule on port 80.
- Go to the Listeners and rules tab of your ALB in the EC2 Dashboard.
Edit the listener associated with your domain (usually HTTP on port 80).
Photo Credit to CodeToFun
Please scroll down slightly and select the Add Rule button.
Photo Credit to CodeToFun
Follow the below steps to add new rule for listener port.
- Name and tags: Type "www to non www" and click on the Next button.
- Define rule conditions: Click on the Add condition button.
- A pop-up window emerges, displaying a dropdown menu for selecting a condition. Opt for the Host header option.
In the Host Header, type your domain name
- www.quizarmy.com
- quizarmy.com
Click on the Confirm button.
Photo Credit to CodeToFun
In the Define rule actions section, perform the following steps:
- Action types: Redirect to URL.
- Redirect to URL: URI parts.
- Custom host, path, query: Tick the checkbox.
- Host: Type your domain name without www.
- Keep the rest unchanged.
Click on the Next button.
Photo Credit to CodeToFun
- In the Priority section, enter 1000 and then proceed by clicking the next button.
- Now, click the Create button.
Congratulations! We have successfully set up redirection from www to non-www for HTTP requests.
Photo Credit to CodeToFun
✏️ Create a New Listener Rule HTTP:443
Follow the steps below to create an HTTP listener rule on port 443.
- Go to the Listeners and rules tab of your ALB in the EC2 Dashboard.
Edit the listener associated with your domain (usually HTTPs on port 443).
Photo Credit to CodeToFun
Please scroll down slightly and select the Add Rule button.
Photo Credit to CodeToFun
Follow the below steps to add new rule for listener port.
- Name and tags: Type "www to non www" and click on the Next button.
- Define rule conditions: Click on the Add condition button.
- A pop-up window emerges, displaying a dropdown menu for selecting a condition. Opt for the Host header option.
In the Host Header, type your domain name
- www.quizarmy.com
Warning: Avoid specifying quizarmy.com in the Host Header within the listener and rules for port 443, as it may result in a Too many redirection error.
Click on the Confirm button.
Photo Credit to CodeToFun
In the Define rule actions section, perform the following steps:
- Action types: Redirect to URL.
- Redirect to URL: URI parts.
- Custom host, path, query: Tick the checkbox.
- Host: Type your domain name without www.
- Keep the rest unchanged.
Click on the Next button.
Photo Credit to CodeToFun
- In the Priority section, enter 1000 and then proceed by clicking the next button.
- Now, click the Create button.
Congratulations! We have successfully set up redirection from www to non-www for HTTPs requests.
Photo Credit to CodeToFun
🔄 Update Route 53 Record Set
- Navigate to the Route 53 Dashboard.
- Select the hosted zone associated with your domain.
- Find and select the record set for your domain (e.g., quizarmy.com).
- Edit the record set.
Update the Alias Target to the newly created target group (NonWwwTargetGroup) from Step 1, as depicted in the image below.
Photo Credit to CodeToFun
- Save the changes.
🕵️♂️ Testing the Redirection
- Open your web browser.
- Enter www.example.com in the address bar.
Verify that the browser is redirected to example.com.
Photo Credit to CodeToFun
🎉 Conclusion
Congratulations! You've successfully set up a redirection from www to non-www using an Application Load Balancer on AWS. This ensures a consistent domain structure and improves the user experience.
Remember to test thoroughly and consider the impact on existing services before making changes in a production environment.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (WWW to Non-WWW Redirection), please comment here. I will help you immediately.