Redirecting to HTTPS and WWW address (non www address redirects to www)

Hello, I’ve looked over the documents and need some help/clarification on my issue. I’m a little stuck with the .htaccess rules and hoping someone with experience can pick up my issue.

I’m using the flexible SSL.

First I set redirection rules .htaccee at my domain. Which caused several redirects and loops. So I deleted everything and set “Page Rules” as follows.

http://example.com/ > ALWAYS USE SSL
This works great. However, I want the domain without WWW to redirect to WWW.

Example:
http://www.example.com > Redirects to https://www.example.com (Correct)

http://example.com > Redirects to https://example.com (INCORRECT - I want it to redirect to the https://www.example.com version

I tried the following .htacess on my server which worked without Cloudflare:

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

I’ve also tried this:

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

Can anyone point out where I’ve gone wrong or if I need to add further rules to identify Cloudflare so that it doesn’t go into a loop.

I understand you can do some of this with forwarding rules in Page Rules. But I would prefer doing this at my server level using .htaccess if possible.And use the page rules instead for bypassing caching etc.

Thanks

Thank you - that makes perfect sense.
And I didn’t know you could get a free SSL from Cloudflare for the origin. So I will enable the full option with the certificate.

Thanks again.

1 Like

.htaccess redirect www to non-www with SSL/HTTPS
I’ve got several domains operating under a single .htaccess file, each secured via SSL. I need to force https on every domain while also ensuring www’s redirect to non-www’s. Here’s what I’m using which doesn’t work:

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST}
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]

Example: https://www.itsmarttricks.com/

should redirect to… https://itsmarttricks.com

2 posts were split to a new topic: Redirect issue