How to Use CloudFlare to Redirect www to Non-www
On this page 5 sections
How to redirect www to non-www in Cloudflare#
To redirect www to non-www in Cloudflare, create a Single Redirect under Rules > Redirect Rules that matches Hostname equals www.thesshguy.com and sends a dynamic 301 to concat("https://thesshguy.com", http.request.uri.path). Keep a proxied DNS record for www so requests reach Cloudflare; the rule does the redirect.
When:
Hostnameequalswww.thesshguy.comThen: dynamic redirect to
concat("https://thesshguy.com", http.request.uri.path)Status code:
301
Have you ever needed to make sure your website works with both www and non-www versions?
I did too!
My website is served at https://thesshguy.com, but I also wanted https://www.thesshguy.com to work.
The Problem with Two Versions#
The first thing I tried was to add A and AAAA DNS records for both versions. While that does work, there's a problem with this approach. Google and other search engines get confused because it looks like there are two versions of my website.
When Google tries to index my website, there's no clear answer to which is the canonical version.
So what can we do instead?
Improve Things with Redirects#
Since we have two versions, we can redirect one to the other to improve things. This tells web crawlers which version is the main one. I used a Cloudflare rule to redirect the www version to the non-www version.
Note: I had to include DNS records and add a redirect rule for things to work correctly.
Here's how you can do it too.
Steps to Set Up the Redirect#
Note: Make sure to use your own website in place of mine.
Log into your Cloudflare account
Go to Rules > Redirect Rules

Click Create rule under Single Redirects
Name your rule (I named mine "WWW to Non WWW")
Set up a custom filter like this:
Field:
HostnameOperator:
equalsValue:
www.thesshguy.comURL redirect Type:
dynamicExpression:
concat("https://thesshguy.com", http.request.uri.path)Status Code:
301(permanent redirect)
Here's what my rule looks like:

How to preserve the query string in a Cloudflare redirect#
The expression above only uses http.request.uri.path, so it drops the query string. A request to https://www.thesshguy.com/blog?ref=twitter redirects to https://thesshguy.com/blog, and ?ref=twitter is lost. That breaks UTM tracking, search pages, and anything else that relies on query parameters.
To keep it, turn on Preserve query string in the rule's redirect settings. Cloudflare then appends the original query string to the URL your expression builds:
Expression:
concat("https://thesshguy.com", http.request.uri.path)Preserve query string: enabled
Status code:
301
Now https://www.thesshguy.com/blog?ref=twitter redirects to https://thesshguy.com/blog?ref=twitter.
Leave the expression as just the path. With Preserve query string on, Cloudflare uses the original request's query string and discards any query string in the target URL, so you don't need to add it to the expression yourself.
If you'd rather not write an expression at all, Cloudflare's Redirect from WWW to Root template does the same thing with a wildcard pattern: match https://www.thesshguy.com/*, redirect to https://thesshguy.com/${1}, with Preserve query string enabled. Either way, Single Redirects are scoped to one zone, so the rule lives in the thesshguy.com zone and covers every path on www.
And that's it!
Now your www version will redirect to the non-www version. Hopefully, this helps you set up your own Cloudflare redirects!