How to setup the www. subdomain to root domain redirect with Cloudflare (Page Rules and DNS Settings)

Published on October 09, 2024

Lukas Himsel Lukas Himsel

In today’s digital landscape, having a consistent domain presence is crucial for your website’s branding and SEO. One common practice is to redirect the “www” subdomain to the root domain. This guide will walk you through the process of setting up this redirect using Cloudflare, a popular content delivery network and web security provider.

Why Redirect WWW to Root Domain?

Before we dive into the how-to, let’s briefly discuss why you might want to implement this redirect:

  1. Consistency: It ensures users always see the same version of your URL, regardless of how they type it.
  2. SEO Benefits: Consolidating to one version can help prevent duplicate content issues.
  3. Simplicity: A shorter, root domain is often easier for users to remember and type.

Step-by-Step Guide

Step 1: Configure DNS Records

First, we need to ensure your DNS records are set up correctly:

  1. Log into your Cloudflare dashboard and select your domain.
  2. Navigate to the DNS section.
  3. Add or update the following records:
    • An A record for the root domain (@) pointing to your server’s IP address
    • A CNAME record for www pointing to your root domain (@)
  4. Make sure both records are proxied through Cloudflare (look for the orange cloud icon).

Screenshot how to create a record

Step 2: Create a Redirect Rule

Now that your DNS is configured, let’s set up the redirect:

  1. In your Cloudflare dashboard, go to the “Rules” section.
  2. Click on “Create Page Rule”.
  3. Set up the rule with these parameters:
    • URL pattern to match: www.yourdomain.com/*
    • In the “Pick a Setting (required)” dropdown, select “Forwarding URL”.
    • Destination URL: https://yourdomain.com/$1
    • Status code: 301 (Permanent Redirect)
  4. Save and deploy the rule.

Screenshot how to create a page rule

Why This Works

This configuration works because:

  1. The DNS records ensure both versions of your domain resolve correctly.
  2. The redirect rule automatically sends visitors from the www version to the root domain.

Conclusion

By following these steps, you’ve successfully set up a redirect from www to your root domain using Cloudflare. This simple change can lead to a more consistent online presence and potentially better SEO performance.

Remember to replace “yourdomain.com” with your actual domain name when implementing these changes. Happy redirecting!

Previous Home Next