If you are a Cloudflare customer you might want to put into use one of the features their 'Page Rules' section has to offer - Permanent Redirects.
Permanent Redirect rules are particularly important for SEO because you can set them in such a way that there aren't duplicate pages on your site, which hurts your ranking in search engines.
So let's dive in...
First off - log into your Cloudflare account and choose your web app:
After that select 'Page Rules' from the tools section:
Now, in order for Cloudflare to handle our www. requests and redirect the client to the non-www url, we should insert the following rule:
The first asterisk is catching requests with either http://www. or https://www. We are hard-coding its replacement to be to our https:// version so that's the substitute to our $1 which acts as a replacement for the first asterisk.
Next, our second asterisk matches the rest of the path, we don't know what that might be and we don't care. We just want to take it and slap it at the end of our 'www' replaced base url.
So this page rule is matching and redirecting with 301 status code the following example requests:
- http://www.somesite.com/ -> https://somesite.com/
- https://www.somesite.com/ -> https://somesite.com/
- http://www.somesite.com/some/path?param1=123 -> https://somesite.com/some/path?param1=123
- https://www.somesite.com/some/path?param1=123 -> https://somesite.com/some/path?param1=123