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 as their content delivery network (CDN) and DNS manager aka Cloudflare users.
Table of Contents
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 it is 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 it. Any misconfiguration in the .htaccess file can lead your whole website to crash!
Also Read: How to Serve Compressed Emoji in WordPress.
Pro tip: Instead of making the forwarding destination ‘https://www.example.com’, make it ‘https://www.example.com/$1’. That way visits to ‘https://example.com/blost-post/3’ will take you to ‘https://www.example.com/blost-post/3’ instead of ”https://www.example.com/”
Cheers
Wow Phil. Thanks for the pro tip! š¤
Phil, that really helped me. Thanks very much.
Nice!
thanks phil. this is exactly what I was looking to do!
Nice, thanks.
You’re welcome Camilo.
I applied the same setting but I get the message site redirects too much
Wait for the page rule settings to propagate across Cloudflare’s servers. Also, you can try clearing your browser’s cache in addition to Cloudflare’s own cache files and then try again. Hope you are able to get it fixed.
Reblogging this on my blog so that someone might find it useful.
Hey Matthews, thanks for re-blogging it.
You’re welcome, friend.
Thanks for following my blog.
You’re welcome!
Informative. Thanks.
You’re welcome Maniparna, hope you found it useful!
Hey guys, thank you very much for your help. It was well put together, detailed written tutorial.
You’re welcome, glad you were able to follow my post and resolve your issue.
Thanks for your time.
You are welcome ÄĆ“ Trį»nh, glad that you found it useful.
Thanks a lot, man!! It works!!
Thank you so much for this. I struggled for hours trying to do what could be done in 5 minutes with the right information.
Hello
It worked properly for me. I appreciate you very much