Over the last years I've applied the same Cloudflare setup pattern to almost every domain I own. It's simple, reliable, and works whether I'm pointing to a traditional server, a PaaS, or a Cloudflare Tunnel. In this article I'll walk through how I configure DNS records and set up a redirect from the naked domain to the www version using Page Rules.

Note: Throughout this article I'll use mydomain.com as an example. Replace it with your actual domain.


1. DNS Configuration in Cloudflare

Once your domain is added to Cloudflare and using Cloudflare's nameservers, the next step is configuring DNS. Cloudflare manages DNS records in the DNS β†’ Records section of the dashboard.

1.1 Open DNS Records for Your Domain

  1. Log in to Cloudflare Dashboard.
  2. Select your domain (for example, mydomain.com).
  3. In the left menu, go to DNS β†’ Records.

You'll see a list of existing DNS records (if any), and a button to Add record.


1.2 Add the A Record for the Root Domain

This record points the root (apex) domain mydomain.com to your server's IP.

  1. On the DNS Records page, click Add record.
  2. Fill the form:
    • Type: A
    • Name: mydomain.com
      (You can also use @ in Cloudflare, which means the root domain.)
    • IPv4 address: 1.2.3.4
      (Replace with your real server IP.)
    • Proxy status: On (orange cloud)
    • Leave TTL as Auto (or your preference).
  3. Click Save.

With Proxy status = On, Cloudflare sits in front of your origin server and provides DDoS protection, caching, WAF, and other optimizations.


1.3 Add the CNAME for www

Next, I almost always serve the public website from www.mydomain.com. I point that hostname either:

  • Directly to my origin (e.g., a hosting provider), or
  • To a Cloudflare Tunnel hostname like 181eb35f-d97a-43ea-b482-023bac8f0a31.cfargotunnel.com.

Steps:

  1. On the same DNS page, click Add record again.
  2. Fill the form:
    • Type: CNAME
    • Name: www
    • Target:
      • Either www.mydomain.com (if this is how your provider expects it), or
      • Your Cloudflare Tunnel hostname, e.g.:
        181eb35f-d97a-43ea-b482-023bac8f0a31.cfargotunnel.com
    • Proxy status: On (orange cloud)
    • TTL: Auto
  3. Click Save.

With a proxied CNAME, traffic to www.mydomain.com is also routed through Cloudflare's edge network, so you get the same protection and performance benefits as the root domain.

Note: Some third-party services require CNAMEs to be DNS only (grey cloud), not proxied. In my pattern here, I proxy the web-facing hostnames I control myself.


2. Redirect the Root Domain to www

I prefer a single canonical host for each site. For public websites, I almost always choose www.mydomain.com as the canonical URL, and then I redirect mydomain.com β†’ www.mydomain.com.

Cloudflare provides URL forwarding via Page Rules (classic) or Redirect Rules (new Rules engine). The pattern is the same: match requests to the root and forward to www with a 301 Permanent Redirect.

Below is the approach using Page Rules, which matches exactly what I normally do.

2.1 Open Page Rules

  1. In the Cloudflare dashboard, select your domain.
  2. Go to Rules β†’ Page Rules.
  3. Click Create Page Rule.

2.2 Create a 301 Redirect Rule for All Root URLs

Now we configure a rule that catches any path on mydomain.com and forwards it to the same path on www.mydomain.com.

  1. In If the URL matches, enter:
    mydomain.com/*

    The * wildcard ensures that any path (e.g. /blog/article-1) is also redirected.

  2. In Pick a Setting, choose Forwarding URL.
  3. For Select status code, choose:
    301 - Permanent Redirect

    This tells browsers and search engines that the canonical version of the site is at www.mydomain.com.

  4. In Enter destination URL, type:
    https://www.mydomain.com/$1
    • $1 captures everything matched by the * in mydomain.com/* and appends it to the destination.
    • Example:
      • Request: https://mydomain.com/blog/article-1
      • Redirects to: https://www.mydomain.com/blog/article-1
  5. Click Save and Deploy.

That's it β€” now all traffic to the naked domain is redirected to the www version, including all paths.

Important: Page Rules (and redirects in general) need the domain/host to be proxied through Cloudflare to take effect. If your DNS record is set to "DNS only", Cloudflare won't see the HTTP request, and the rule won't run.


3. Why I Use This Setup Across Most Domains

A quick summary of why this has become my default template:

  • Consistent canonical URLs
    One host (www) for the website simplifies SEO, analytics, and link sharing. Everything redirects cleanly to the same host.
  • Cloudflare protection everywhere
    With proxy enabled on both the root and www:
    • My origin IP is hidden.
    • I get DDoS protection, WAF, caching, and performance optimizations by default.
  • Works great with tunnels and SaaS
    When I use Cloudflare Tunnel or SaaS platforms that support CNAME hosting, I can still keep Cloudflare in front, sometimes even using Orange-to-Orange (O2O) routing where both sides use Cloudflare.
  • Simple mental model
    • A record for the root (mydomain.com β†’ IP)
    • CNAME record for www (β†’ origin or tunnel)
    • A single Page Rule to redirect mydomain.com/* β†’ https://www.mydomain.com/$1

Once you do it a couple of times, it becomes a 2–3 minute operation for each new domain.


Summary

This is the pattern I use for almost every domain I manage on Cloudflare:

  1. Add an A record for the root domain pointing to your server IP with proxy enabled (orange cloud).
  2. Add a CNAME record for www pointing to your origin or Cloudflare Tunnel, also proxied.
  3. Create a Page Rule to redirect mydomain.com/* to https://www.mydomain.com/$1 with a 301 redirect.

This setup gives you:

  • A single canonical URL (www)
  • Full Cloudflare protection (DDoS, WAF, caching)
  • Hidden origin IP
  • SEO-friendly 301 redirects
  • Flexibility to use traditional hosting or Cloudflare Tunnels

It's simple, reliable, and has served me well across dozens of domains.


References