How to setup the www. subdomain to root domain redirect with Cloudflare (Page Rules and DNS Settings)
Published on October 09, 2024
Lukas HimselIn 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:
- Consistency: It ensures users always see the same version of your URL, regardless of how they type it.
- SEO Benefits: Consolidating to one version can help prevent duplicate content issues.
- 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:
- Log into your Cloudflare dashboard and select your domain.
- Navigate to the DNS section.
- 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 (@)
- Make sure both records are proxied through Cloudflare (look for the orange cloud icon).
Step 2: Create a Redirect Rule
Now that your DNS is configured, let’s set up the redirect:
- In your Cloudflare dashboard, go to the “Rules” section.
- Click on “Create Page Rule”.
- 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)
- URL pattern to match:
- Save and deploy the rule.
Why This Works
This configuration works because:
- The DNS records ensure both versions of your domain resolve correctly.
- 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!