Configuring Cloudflare to Redirect WWW to Apex Domain
- 1 min read
Congratulations! You are setup and using cloudflare for your DNS. Now is a good time to ensure that if people who are typing in your domain can actually get to your site.
Some people are in the habit of typing www in front of the domain name even though the www is not actually being used. To make sure your users are reaching your site with an error, we need to setup a redirect.
Fortunately, this is straight-forward and only takes a minute to setup.
Step 1
Log into your cloudflare account. In the left navigation at the bottom you will see a bulk redirects menu item. Click that and you will be presented with a screen similar to this.
This is what it will look like when we are done. For now, let's get startedy by clicking the Create Bulk Redirect List button.
Step 2
We want to provide a useful name. I have chosen to call this domain_redirects.
After entering the name, click the Next button.
Step 3
You could now upload a list of redirects, but this is beyond the purpose of this article. Instead click the link to maually add URL redirects.
Step 4
Enter the source URL. The format here is www.yourdomain.com/ ... The slash is important because webserver rules see a "/" as a directory, so this rule will catch everything from the root of the site "/" . In the target, specify your website, https://yourdomain.com. The status of 301 is what we want.
Then expand the edit parameters option and ensure the following items are selected:
Preserve query string
Subpath matching
Preserve path suffix
Then click Next.
Step 5
Confirm your details are correct and click Next.
Step 6
Now we need to create a rule to which we will apply this list. Click the Continue to Redirect Rules button.
Step 7
We need to provide a name, and I recommend www to apex as the name as this makes it easy to remember the purpose of the rule. The domain_redirects list we created will be selected as it is likely the only list you have.
Click the Save and Deploy button and that's it. We are done. You can now test that your redirect is properly configured.
For more information you can always refer to the cloudflare documentation for bulk redirects.