CrazzyMarc
CrazzyMarc

Reputation: 33

HTTPS redirect not working without www

I added SSL with CloudFlare to my website.
When I enter the website with www.example.com everything goes well.
But when I enter the website with example.com it will not redirect to HTTPS.


.htaccess

RewriteEngine On

# RewriteRules
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301]

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=302,NE,L]

# ErrorDocument
ErrorDocument 404 /404.php

I don't understand a lot from htaccess, I hope you can help.
The goal is to have http://www.example.com and http://example.com redirect to https://example.com.

Upvotes: 0

Views: 1433

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

Change your RewriteCondition to

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]

to allow both www or non-www

Upvotes: 0

Croises
Croises

Reputation: 18671

With CloudFlare, use:

# RewriteRules
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://exemple.com%{REQUEST_URI} [NE,R=301,L]

Instead of your first RewriteCond,RewriteRule 2 lines

Upvotes: 2

Related Questions