Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

WWW to Non-WWW Redirection

Posted in AWS Tutorial
Updated on Dec 30, 2023
By Mari Selvan
👁️ 40 - Views
⏳ 4 mins
💬 1 Comment
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:

  1. An AWS account with access to the AWS Management Console.
  2. 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.

  1. Go to the Listeners and rules tab of your ALB in the EC2 Dashboard.
  2. Edit the listener associated with your domain (usually HTTP on port 80).

    listener HTTP:80

    Photo Credit to CodeToFun

  3. Please scroll down slightly and select the Add Rule button.

    Add Listener Rule

    Photo Credit to CodeToFun

  4. Follow the below steps to add new rule for listener port.

    1. Name and tags: Type "www to non www" and click on the Next button.
    2. Define rule conditions: Click on the Add condition button.
    3. A pop-up window emerges, displaying a dropdown menu for selecting a condition. Opt for the Host header option.
    4. In the Host Header, type your domain name

      • www.quizarmy.com
      • quizarmy.com

      Click on the Confirm button.

      Add condition for listener rule

      Photo Credit to CodeToFun

  5. In the Define rule actions section, perform the following steps:

    1. Action types: Redirect to URL.
    2. Redirect to URL: URI parts.
    3. Custom host, path, query: Tick the checkbox.
    4. Host: Type your domain name without www.
    5. Keep the rest unchanged.

    Click on the Next button.

    Define rule actions

    Photo Credit to CodeToFun

  6. In the Priority section, enter 1000 and then proceed by clicking the next button.
  7. Now, click the Create button.
  8. Congratulations! We have successfully set up redirection from www to non-www for HTTP requests.

    Application Load Balancer successful redirection www to non www for http

    Photo Credit to CodeToFun

✏️ Create a New Listener Rule HTTP:443

Follow the steps below to create an HTTP listener rule on port 443.

  1. Go to the Listeners and rules tab of your ALB in the EC2 Dashboard.
  2. Edit the listener associated with your domain (usually HTTPs on port 443).

    listener HTTPS:443

    Photo Credit to CodeToFun

  3. Please scroll down slightly and select the Add Rule button.

    Add Listener Rule

    Photo Credit to CodeToFun

  4. Follow the below steps to add new rule for listener port.

    1. Name and tags: Type "www to non www" and click on the Next button.
    2. Define rule conditions: Click on the Add condition button.
    3. A pop-up window emerges, displaying a dropdown menu for selecting a condition. Opt for the Host header option.
    4. 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.

      Add condition for listener rule for https

      Photo Credit to CodeToFun

  5. In the Define rule actions section, perform the following steps:

    1. Action types: Redirect to URL.
    2. Redirect to URL: URI parts.
    3. Custom host, path, query: Tick the checkbox.
    4. Host: Type your domain name without www.
    5. Keep the rest unchanged.

    Click on the Next button.

    Define rule actions

    Photo Credit to CodeToFun

  6. In the Priority section, enter 1000 and then proceed by clicking the next button.
  7. Now, click the Create button.
  8. Congratulations! We have successfully set up redirection from www to non-www for HTTPs requests.

    Application Load Balancer successful redirection www to non www for http

    Photo Credit to CodeToFun

🔄 Update Route 53 Record Set

  1. Navigate to the Route 53 Dashboard.
  2. Select the hosted zone associated with your domain.
  3. Find and select the record set for your domain (e.g., quizarmy.com).
  4. Edit the record set.
  5. Update the Alias Target to the newly created target group (NonWwwTargetGroup) from Step 1, as depicted in the image below.

    www to non-www hosted zone

    Photo Credit to CodeToFun

  6. Save the changes.

🕵️‍♂️ Testing the Redirection

  1. Open your web browser.
  2. Enter www.example.com in the address bar.
  3. Verify that the browser is redirected to example.com.

    Domain serving via non www

    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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
3 months ago

If you have any doubts regarding this article (WWW to Non-WWW Redirection), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy