1
votes

I have a website on shared host, the hosting default root directory is /public_html, but I place my website at /pulbic_html/web for file organizing purpose

I have registered for HTTPS from cloudflare

I wish for 2 results

  1. visitors can type https://example.com, https://www.example.com
  2. visitors can type http://example.com, http://www.example.com then redirect to either https://example.com or https://www.example.com

My current .htaccess

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

Using the current .htaccess

  1. typing http://example.com -> working
  2. typing http://www.example.com -> working
  3. typing https://example.com -> working
  4. typing https://www.example.com -> working but redirect to http://example.com with connection not secure warning on the browser

I have googled and came up with

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

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

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

with this .htaccess all testing result

enter image description here

please help to achieve these

  1. typing https://example.com, https://www.example.com -> working
  2. typing http://example.com, http://www.example.com -> redirect to either https://example.com or https://www.example.com

thanks all

2

2 Answers

1
votes
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
1
votes

This might help.. Update your current rule with this:

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