How to Redirect non-www URLs to www using Cloudflare

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 👉Cloudflare Pagerules

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.

redirect non-www to www urls settings for cloudflare

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.

2 thoughts on “How to Redirect non-www URLs to www using Cloudflare

  1. 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

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s