Redirecting Root Domain with Cloudflare

Derek ArmstrongDerek Armstrong
6 min read

You know the old saying, "It’s always DNS"? Well, it's true. And the more you dabble in the wonderful world of web management, the more you realize just how much your digital kingdom rests on those little DNS settings. Whether you’re managing a massive web empire or just tinkering with your personal site, knowing how to tame your network infrastructure is like having the keys to the castle. So let's get into it and setup a redirect at the root level domain together!

Example Scenarios

Redirecting a root domain using Cloudflare is a common practice in various scenarios, especially when managing web traffic, branding, or SEO. Here are some scenarios where someone might need to do this:

1. Branding and Consistency

  • Redirecting Non-www to www: Because we all want to look sharp and consistent, right? You might want all traffic to go to www.example.com instead of example.com. This helps ensure that users always see the same version of your site, like having all your socks match—because mismatched URLs are just as bad as mismatched socks.

  • Redirecting www to Non-www: Or maybe you’re a minimalist and prefer the sleek, no-frills example.com as your primary URL. In that case, you’ll be redirecting everything from www.example.com to example.com. Just like ditching that unnecessary "www" clutter.

2. Domain Consolidation

  • Merging Multiple Domains: Got a handful of domains (e.g., example1.com, example2.com)? It’s like trying to herd cats. Consolidate them by redirecting all traffic to a single domain (example.com), so everything points in the same direction. It’s domain feng shui.

  • Rebranding: Changing your domain from oldbrand.com to newbrand.com? Redirect the old root domain to the new one to keep all your loyal visitors (and your SEO rankings) in tow. Think of it like forwarding your mail after moving to a fancy new digital address.

3. SEO and Duplicate Content

  • Avoiding Duplicate Content: Google frowns upon duplicate content like a librarian shushing a noisy visitor. To avoid penalties, you’ll want to redirect all traffic to a single canonical URL (e.g., example.com redirects to www.example.com). Because let’s be honest, duplicate content is just unnecessary noise.

  • Maintaining SEO Rankings: Migrating to a new domain? Redirect the old root domain to the new one, and watch as your hard-earned SEO rankings follow you to your shiny new home. It’s like changing the address on your business card but with way more techy jargon involved.

4. Traffic Management

  • Temporary Redirects During Maintenance: Sometimes your website needs a little TLC. Set up a temporary redirect to a maintenance page (maintenance.example.com) so your visitors know you haven’t vanished into the digital abyss.

  • Redirecting to a Regional or Language-Specific Site: Got a global audience? Redirect users from the root domain to region-specific sites (example.com to us.example.com for U.S. visitors). It’s like rolling out the red carpet, tailored to each visitor.

5. Domain Migration

  • Moving to a New TLD: Making the leap from .com to .dev? Redirect the old domain (example.com) to the new one (example.dev), and keep everyone on the right track. Think of it as leaving breadcrumbs for your visitors to follow.

  • Transition from a Development or Staging Environment: Your site is graduating from staging (staging.example.com) to production! Redirect the root domain to ensure all traffic flows to your up-to-date masterpiece.

6. Preventing Domain Squatting

  • Protecting a Brand: Own multiple variations of your domain to protect your brand? Redirect those variations to the main site to prevent confusion and keep domain squatters at bay. Because nobody likes a digital trespasser.

7. Simple URL Forwarding

  • Redirecting to a Social Media Profile: Not quite ready for a full website? Redirect your root domain to a social media profile (example.com to facebook.com/yourprofile). It’s like handing out your business card, but online.

  • Redirecting to a Third-Party Service: If your domain is just a gateway to another service (e.g., a blog hosted on Medium or a store on Shopify), redirect it there (example.com to yourstore.shopify.com). Because who said you need to build everything from scratch?

8. Dealing with Expired Services

  • Redirecting After Expiring or Discontinuing a Service: If a service or site is no more, its root domain can be redirected to another service or a page explaining the change. It’s like leaving a "We’ve Moved" sign up for your digital neighbors.

Step-by-Step Guide to Redirect a Root Domain Using Cloudflare

Alright, enough scenarios. Let’s get down to business and set up that redirect!

1. Log in to Cloudflare

First things first, log in to your Cloudflare dashboard.

2. Select Your Domain

From your list of domains, click on the one you want to redirect.

3. Set Up DNS Records

To make sure traffic to example.com flows smoothly through Cloudflare, set up these DNS records:

  • Root Domain A Record (example.com):

    • Type: A

    • Name: @

    • IPv4 Address: 192.0.2.1 (Just a placeholder, doesn't actually matter)

    • TTL: Auto

    • Proxy status: Proxied (Orange Cloud)

  • www Subdomain A Record (www.example.com):

    • Type: A

    • Name: www

    • IPv4 Address: Same as above (192.0.2.1)

    • TTL: Auto

    • Proxy status: Proxied (Orange Cloud)

Pro Tip: Prefer a CNAME? No problem. Use this instead:

  • CNAME Record for Root Domain:

    • Type: CNAME

    • Name: @

    • Target: www.example.com

    • TTL: Auto

    • Proxy status: Proxied (Orange Cloud)

4. Create a Page Rule for the Redirect

Now for the fun part: setting up the Page Rule to actually do the redirecting.

  1. In your domain’s dashboard, head over to Page Rules.

  2. Click Create Page Rule.

  3. In the "If the URL matches" field, enter example.com/* (This little wildcard * will capture everything after the root domain).

  4. Under "Then the settings are", select Forwarding URL.

  5. Choose your redirect type:

    • 301 - Permanent Redirect: This is your "Forever" option.

    • 302 - Temporary Redirect: Just temporary? Use this.

  6. In the "Enter destination URL" field, type the URL you want to send folks to, like https://www.example.com/$1

    1. The $1 is the bit that keeps all your existing links like example.com/some/path working. In short it keeps your old paths intact.
  7. Click Save and Deploy.

5. Test the Redirect

Now’s the moment of truth. Go to example.com in your browser and make sure you land at https://www.example.com or wherever you’ve decided to send your traffic. If it works, give yourself a high-five—you’ve just mastered DNS redirection like a pro.

Summary Example Configuration

Here’s a quick recap of what you’ve just done:

  • DNS Settings:

    • A Record: @192.0.2.1 (Proxied)

    • A Record: www192.0.2.1 (Proxied)

  • Page Rule:

Congrats! By following these steps, you’ve ensured that your root domain is successfully redirected using Cloudflare’s DNS and Page Rules features. Go forth and redirect with confidence—because when it comes to DNS, you’re now in the driver’s seat!

4
Subscribe to my newsletter

Read articles from Derek Armstrong directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Derek Armstrong
Derek Armstrong

I share my thoughts on software development and systems engineering, along with practical soft skills and friendly advice. My goal is to inspire others, spark ideas, and discover new passions.