What is the Cloudflare equivalent of these two .htaccess redirect rules?

by Knocks X   Last Updated September 10, 2019 14:04 PM - source

I usually set up my sites to rewrite URLs in the following cases:

  1. All http traffic is redirected to https (and www), with a 301
  2. All non-www traffic is redirected to www (and https), with a 301

In .htaccess, the rewrite rules are as follows:

RewriteEngine on
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Note that all redirects are achieved with only one hop. Non-www traffic goes to www and https, and non-https traffic goes to https and www. How do you achieve the same results in Cloudflare?

P.S. I'm not sure if Cloudflare's "Always Use HTTPS" option is worth enabling since it doesn't use a 301 and I have to write a separate rule for a https redirect rule with a 301 anyway.



Related Questions



Are these 3 redirect rules equal?

Updated July 16, 2018 09:04 AM

Forcing SSL and www in .htaccess

Updated July 21, 2016 08:01 AM

How to force ssl and www for subdomains?

Updated July 08, 2018 17:04 PM

Subdomain redirects https:// to http://

Updated May 28, 2016 08:01 AM