Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess file not redirecting http://www. to https://www

Tags:

I have made a .htaccess file to redirect all website traffic to https://www..

This is my complete .htaccess file:

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

The below redirects work exactly as expected:

http://example.com -> https://www.example.com
https://example.com -> https://www.example.com
https://www.example.com -> https://www.example.com

Except:

http://www.example.com -> http://www.example.com

As shown above, if you go to http://www. it doesn't redirect to the HTTPS version.

Can anyone help me understand why the other redirects are working fine, but that one is not?

Additional Notes: I have looked at a number of posts on StackOverflow, but most of their solutions end in redirect loop errors.

like image 581
frostluke Avatar asked Apr 12 '17 11:04

frostluke


People also ask

How do I force redirect to www?

Under Type, select the Permanent (301) option. On https?://, enter the domain you want to redirect. Leave the path section (/) empty. In the Redirects to field, type in your website's www URL.


1 Answers

After contacting 123-Reg (my hosting provider), they submitted this solution, which works perfectly:

RewriteEngine on

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

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

Basically they have set the script to do two tasks: Change domain to WWW, if it isn't already, THEN change to HTTPS. Also, they used ENV:HTTPS, which is different to what was found in their documentation (ENV:SSL).

Glad to have to this sorted, and maybe this will help out others using 123-Reg Hosting.

like image 139
frostluke Avatar answered Sep 23 '22 10:09

frostluke