One hop http to https Redirects in Cloudflare for www or non-www

Performance is often about not doing unnecessary things and redirects are no exception! When running speed tests on gtmetrix, pingdom or webpagetest, you may see warnings about unnecessary redirects especially if you have migrated from http to https or have changed your domain from non-www to www. This guide has Cloudflare users covered to make sure no unnecessary redirects are happening for your domain so it is just a single hop and not multiple hops by fixing the redirect chain.

For example, if we have a domain https://wp-bullet.com, but a user enters http://www.wp-bullet.com, what will happen by default is that http://www.wp-bullet.com will redirect to https://www.wp-bullet.com and then to https://wp-bullet.com. What we want is for http://www.wp-bullet.com, https://ww.wp-bullet.com and http://wp-bullet.com to all redirect to https://wp-bullet.com in a single hop.

non-www Domain

For non-www e.g. if your site is https://wp-bullet.com

First we will redirect http://wp-bullet.com/ to https://wp-bullet.com/

Log in to Cloudflare https://dash.cloudflare.com/

Go to the Page Rules tab in the dashboard and click Create Page Rule

In If the URL matches put http://wp-bullet.com/*

Choose Forwarding URL and 301 – Permanent Redirect

Finally put https://wp-bullet.com/$1 for the Forwarding URL value

Click Save and Deploy

Now we need to redirect www.wp-bullet.com/ to https://wp-bullet.com/

Click Create Page Rule again

In If the URL matches put www.wp-bullet.com/*

Choose Forwarding URL and 301 – Permanent Redirect

Finally put https://wp-bullet.com/$1 for the Forwarding URL value

Click Save and Deploy

Testing Single Hop Redirects for non-www Domain

Testing http://www.wp-bullet.com redirects to https://wp-bullet.com

curl -I http://www.wp-bullet.com

Look for the Location header and notice it is pointing to https://wp-bullet.com and not https://www.wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 12:07:43 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://wp-bullet.com/
Server: cloudflare
CF-RAY: 452f60e0c67fbd98-AMS

Testing https://www.wp-bullet.com redirects to https://wp-bullet.com

curl -I https://www.wp-bullet.com

Look for the Location header and notice it is pointing to https://wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 12:07:50 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://wp-bullet.com/
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 452f6109ec65bf5c-AMS

Testing http://wp-bullet.com redirects to https://wp-bullet.com

curl -I http://wp-bullet.com

Look for the Location header and see it is pointing to https://wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 12:08:04 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://wp-bullet.com/
Server: cloudflare
CF-RAY: 452f616375809d08-AMS

www Domain

For www e.g. if your site is https://www.wp-bullet.com

First we will redirect http://www.wp-bullet.com/ to https://www.wp-bullet.com/

Log in to Cloudflare https://dash.cloudflare.com/

Go to the Page Rules tab in the dashboard and click Create Page Rule

In If the URL matches put http://www.wp-bullet.com/*

Choose Forwarding URL and 301 – Permanent Redirect

Finally put https://www.wp-bullet.com/$1 for the Forwarding URL value

Click Save and Deploy

Now we need to redirect wp-bullet.com/ to https://www.wp-bullet.com/

Click Create Page Rule again

In If the URL matches put wp-bullet.com/*

Choose Forwarding URL and 301 – Permanent Redirect

Finally put https://www.wp-bullet.com/$1 for the Forwarding URL value

Click Save and Deploy

Testing Single Hop Redirects for www Domain

Testing http://wp-bullet.com redirects to https://www.wp-bullet.com

curl -I http://wp-bullet.com

Look for the Location header and notice it is pointing to https://www.wp-bullet.com and not https://wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 11:53:41 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://www.wp-bullet.com/
Server: cloudflare
CF-RAY: 452f4c5176ad9cde-AMS

Testing https://wp-bullet.com redirects to https://www.wp-bullet.com

curl -I https://wp-bullet.com

Look for the Location header and see it is pointing to https:/www.wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 11:54:06 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://www.wp-bullet.com/
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 452f4cebd9479ca1-AMS

Testing http://www.wp-bullet.com redirects to https://www.wp-bullet.com

>curl -I http://www.wp-bullet.com

Look for the Location header and see it is pointing to https:/www.wp-bullet.com

HTTP/1.1 301 Moved Temporarily
Date: Fri, 31 Aug 2018 11:56:38 GMT
Connection: keep-alive
Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Location: https://www.wp-bullet.com/
Server: cloudflare
CF-RAY: 452f50a500a59cad-AMS

That should take care of any unnecessary redirects from http to https for non-ww or www domains!