Search code examples
apachehttpscloudflare

Cloudflare redirect to https://www


I enabled SSL in cloudflare on Flexible option, and I set Automatic HTTPS rewrites to ON.

I want the following:

example.com

www.example.com

http://example.com

http://www.example.com

to redirect all of them to

https://www.example.com

What are the redirect rules that I have to add inside cloudflare website? I tried to write rules inside .htaccess but it didn't work.


Solution

  • RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]