Self-Hosting Guides  ->  Beyond the Basics

Redirect HTTP & WWW

Checklist

Introduction

This guide will walk you through redirecting all insecure (HTTP) traffic, as well as all WWW traffic, to https://example.com.

We do this because there are 4 (unofficial) standard variations of a URL that users should be able to access your website through, but we only want one of them to be the "official" URL, which helps with both search engine optimization as well as user experience.

Below is a list of the 4 URL variations. We're going to use variation #1 (the secure, non-www version) as our official URL, and then we're going to redirect all the other variations to it.

Variation Secure www URL Official URL?
1 Yes No https://patiopi.com Yes
2 Yes Yes https://www.patiopi.com No
3 No No http://patiopi.com No
4 No Yes http://www.patiopi.com No

Based on the below HTTP Header tests, variations #1 and #3 are displaying the same content, which isn't ideal for search engines, and variations #2 and #4 don't even work.

HTTP headers (bad)

Create Redirect Rules

In order to create the redirects we mentioned above, we're going to use Cloudflare's Redirect Rules feature. This feature allows us to create rules that will redirect users from one URL to another based on a set of conditions.

Click on Account Home in the top left-hand corner of the Cloudflare dashboard, and then click on your domain.

Account Home, Domain

Scroll down the main menu until you see the Rules section, then click on it to expand its sub-menu. On the sub-menu click Redirect Rules.

Redirect Rules

After the Redirect Rules page opens, click the Create rule button.

Create rule

Fill in the following details and click Deploy to create your first redirect rule. This will redirect https://www.patiopi.com to https://patiopi.com.

Rule Name: https://www.* -> https://${1}
Redirect Type: Wildcard pattern
Request URL: https://www.*
Target URL: https://${1}
Status Code: 301
Preserve query string: Selected

Create first rule

Click Create rule again, then fill in the following details and click Deploy to create your second redirect rule. This will redirect http://www.patiopi.com to https://patiopi.com.

Rule Name: http://www.* -> https://${1}
Redirect Type: Wildcard pattern
Request URL: http://www.*
Target URL: https://${1}
Status Code: 301
Preserve query string: Selected
Place at/Select order: Last

Create second rule

Click Create rule one more time, then fill in the following details and click Deploy to create your final redirect rule. This will redirect http://patiopi.com to https://patiopi.com.

Rule Name: http://* -> https://${1}
Redirect Type: Wildcard pattern
Request URL: http://*
Target URL: https://${1}
Status Code: 301
Preserve query string: Selected
Place at/Select order: Last

Create third rule

You'll now be taken back to the Redirect Rules page where you can see a complete list of your rules. Confirm that everything looks ok and that all rules are enabled.

Confirm rules

Create a DNS Record

Now that our redirect rules have been created, we need to create a DNS record to point www.patiopi.com to the same location as patiopi.com, which will allow our two WWW redirect rules to work properly.

Click on Account Home in the top left-hand corner of the Cloudflare dashboard, and then click on your domain.

Account Home, Domain

Once your domain's overview page has loaded, click on DNS Records.

DNS Records

Click on Add record.

Add record

Fill in the following details and click Save to create your new DNS record:

Type: CNAME
Name: www
Target: patiopi.com
Proxy Status: ON

Save record

You should now be taken back to your DNS Records page, and the new DNS record you created should be visible on the list.

View new DNS record

And now if we test the 4 URL variations again using an HTTP header tester, we should see that they all redirect to https://patiopi.com, which is now the "official"/only actual live website.

HTTP headers (good)


HTTP & WWW Redirects Setup

Congratulations, your new Cloudflare Redirect Rules are setup and all 4 of your standard URL variations are now redirected to your official URL!