Domain Redirects in  CloudFlare

Redirect an entire domain in CloudFlare, forwarding all non-WWW traffic to WWW, or WWW to the root domain, at the page level with a single rule.

The proper way to redirect a root domain (non-www) to the equivalent on the www. subdomain URL is with a single page-level 301 redirect. Page level means that if someone visits https://domain.com/about/ we want them to be redirected to https://www.domain.com/about/.

Like wise, if we want all www URL’s to the root domain, then when someone visited https://www.domain.com/about/ we’d want them to be redirected to https://domain.com/about/. Many of the other tutorials I see documented around the web do not do this, which is bad for UX and SEO.

Page Rule Redirects

The best way to handle this in CloudFlare is with page rules that set a forwarding URL. Wildcard variables make it possible to handle a page to page redirect with a single rule. Wildcards are added to the URL field as a * character, and each one corresponds to a variable we can reference in the forwarding address. Variables are used in the destination URL with a $ followed by a number starting at 1, so to refer to the first wildcard we use $1, $2 for the second wildcard, and so on. In most cases, only one is used, but it’s good to know how it works for more advanced scenarios.

CloudFlare Page Rules: Existing Non-WWW Root to WWW Subdomain 301 Redirect

Non-WWW to WWW

Here is an example page rule that will redirect all URL’s from a root domain (domain.com) to the corresponding URL on the www subdomain (www.domain.com).

  • URL: domain.com/*
  • Pick a Setting: Forwarding URL
  • Select status code: 301 – Permanent Redirect
  • Enter destination URL: https://www.domain.com/$1

To use this replace domain.com with your own domain, then click Save Page Rule. That’s it, you’ll redirect all URL’s to the root domain of your website automatically with a single 301 redirect rule.

CloudFlare Page Rules: Redirect root domain to WWW with a 301 redirect

WWW to Non-WWW

Here is an example page rule that will redirect all URL’s from a www subdomain to the corresponding URL on the root domain (domain.com).

  • URL: www.domain.com/*
  • Pick a Setting: Forwarding URL
  • Select status code: 301 – Permanent Redirect
  • Enter destination URL: https://domain.com/$1

To use this, replace domain.com with your own domain, then click Save Page Rule. That’s it, you’ll redirect all URL’s to the root domain of your website automatically with a single 301 redirect rule.

CloudFlare Page Rules: Redirect WWW to root domain with a 301 redirect

Case For 301 Redirects

Why should we do this at all? Well, you don’t have to. You could have your site load the same content at two URL’s, and if you properly set a canonical tag it’s not an SEO issue.

But it’s almost always a bad idea that adds unnecessary complexity:

  1. Cookies need to be properly setup as a root level domain, which will likely require modifications and added complexity to your website and web apps
  2. Analytics and access logs are more difficult to decipher and work with. They can be normalized, but why add complexity for no reason?
  3. Consistency for users: it’s nice for people to always see the same URL root when they copy, visit or share a URL.
  4. SSL certificates: You’ll need to have an SSL certificate that covers both domains, or a root level that covers all subdomains
  5. Site level adjustments to properly set canonical tags for SEO

Overall there’s just no compelling reason to not redirect and keep things consistent. There may be extreme edge cases out there, but in 99% of the scenarios I’ve seen it’s best to use a 301 to or from your root domain and www..

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.