Htaccess redirect from http to https for cloudflare vistors

When I Enable 301 .htaccess redirect in really simple ssl, the site crashes. I had gone through this link article on setting it manually, https://really-simple-ssl.com/knowledge-base/manually-insert-htaccess-redirect-http-to-https/ . Which setting code should I use to redirect using htaccess. If I use Always enable HTTPS in Cloudflare, it works but it doesn’t redirect properly to my mobile theme and desktop theme at times. But I am thinking that using redirect in htaccess, it should work.
Here is how my htaccess code looks. I use www version of my site and use cloudflare flexible SSL.

#CrazyDomains Start

This block is generated automatically.

Do not make any changes to the section between marks “CrazyDomains Start” and “CrazyDomains End”.

This section was created to bind multiple domains to one Wordpress site.

When you move the Productions to the Staging, everything between marks “CrazyDomains Start” and “CrazyDomains End” will be deleted.

When you move the Staging to the Production this section will be created again.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http%1://www.example.com/$1 [L,R,NE]
#CrazyDomains End

What should I insert in my above .htaccess to redirect http to https automatically for Cloudflare visitors?

Thank you.

First question: why? Is there a valid certificate on your host?

I don’t know your setup, but if it says :don’t touch that, don’t touch it :wink:
What does that mean? Too many redirects?

Would you mind telling us the domain?

My host asked me to use flexible SSL and hence I am using it. With cloudflare’s Always use HTTPS, the redirect works perfectly but since I have mobile theme and desktop theme on same url, it doesn’t load correctly. So, I had to disable Cloudflare’s Always use HTTPS. Also, I wanted to mention that I use wp super cache.

Btw, I searched online for htaccess rules regarding http to https and non-www to www and I was able to come up with this.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP:CF-Visitor} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

I added it to the htaccess, it works correctly and I am able to see mobile theme on mobile devices and desktop theme on desktops.
Here are the 301 jumps on how it is working
example.com -> https://example.com -> https://www.example.com

But the problem is with www.example.com url.
Most of the time 301 works correctly like
www.example.com -> https://www.example.com
But sometimes the 301 redirect goes wrong, like www.example.com doesn’t redirect any further(http to https). I am thinking if there is any way to correctly order the above code in htaccess?
Thank you in advance.