Usually when setting up DNS, you point your domain and the www subdomain to the same address. Meaning, the same website will be accessed on both non-www and www version.
This can cause issues in some web apps such as SEO problems. Crawlers identify non-www and www version of the website as different website, but with the same content. Google and similar search engines gives penalty for duplicate content.
This can be usually be fixed by adding
<link rel="canonical" href="" /> on your website so that search engines know the original source.
Another problem that I have encountered when dealing with both non-www and www version is that web app sessions don’t persist at the same time. Meaning you can be logged in on www version but not the other and vice versa.
Personally I prefer the www version of my website, that I redirect users from non-www version to www version.
Head over to the official website of Cloudflare and click Log In in the top menu.
Enter your credentials and you should be redirected to your dashboard.
On your dashboard, it should list all registered domains on your Cloudflare account.
On your left menu, click Rules, under it, click Page Rules.
On that page, click Create Page Rule button.
On the URL (required) field input your domain like this:
* to match all content of our website. The end
* matches different pages on my website.
davidangulo.xyz with your own domain name, also
http if your website does not have an SSL certificate. However, I recommend getting an SSL certificate first by following How to get free SSL certificate from Cloudflare.
Then for Pick a Setting (required) field, select Forwarding URL.
For Select status code (required) field, select 301 - Permanent Redirect, you can also use
302 if it’s only temporary.
For Enter destination URL (required) field:
$1 represents the data that we get when using
* has a value of
$1 will also have the same value.
To finalize, click Save Page Rule.
After a few minutes try accessing your website, and it should now be redirected to www version if it’s accessed on a non-www version.
If you have an Always Use HTTPS rule, you might want to put Forwarding URL rule under it. So that it redirects to the
https version first and then redirect to www version.