Here’s how to redirect the non-www URLs of your website to the www version using Cloudflare without touching your .htaccess file. Most of the websites are accessible using both, the non-www URLs and the www URLs. But usually people don’t prefer that in order to comply with the best SEO practices. This is because search engines consider URLs with and without “www” as two different websites or duplicate content and this can lead to a penalty.
NOTE: This post applies only to people who are using Cloudflare services.
Step 1 –
Login to your Cloudflare account on which the website for which you want to set redirects is active.
Step 2 –
Click on the ‘Page Rules‘ tab in your Cloudflare dashboard. It should look something like this 👉
Step 3 –
Then click the ‘Create Page Rule‘ button. Set the page rule as ‘Forwarding URL‘. Now have a look at the screenshot below and replace ‘yourgroovywebsite.com’ with your own website’s domain name and extension.
Step 4 –
Once you’ve replaced ‘yourgrrovywebsite.com’ with your domain name, hit ‘Save and Deploy‘. Make sure that you have set the forwarding type as 301 – Permanent Redirect. Also set the Order (not seen in the pic but you will be given that option when adding the page rule if you’ve already set any page rules before this) as First. This is the order in which your Page Rules will be triggered. Only one Page Rule will trigger per URL, so put we must put this page rule at the top.
Step 5 –
We are nearly done but just to clear the previous settings, purge your cache (though it’s not absolutely necessary) from your Cloudflare dashboard. To test if its working properly, clear the saved files and cache from your browser and then try visiting the non-www URL of your website from the address bar and you will find it automatically getting redirected to the www version by default.
That’s it. Now all requests to the non-www version of your site will be getting redirected automatically to the www version. Rest is handled by Cloudflare.
If you still insist on creating the redirection with the help of .htaccess file, then follow this tutorial by SiteGround. Caution: Download a backup of your .htaccess file before making any changes to the file. Any misconfiguration in the .htaccess file can lead your whole website to crash!
Also Read: How to Serve Compressed Emoji in WordPress.