I installed basic Yii2 application and want to force all connections to use HTTPS instead of HTTP. Here my .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule (.+)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/main\.html
RewriteRule ^(.*)main.html /$1 [R=301,L]
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
And in web/index.php I'm adding
$_SERVER['HTTPS']='on';
When I open site I see an error "too many redirects". I check chrome network inspector and I see that from https://example.org I'm redirecting to https://example.org and so get into the cycle
I used following code to redirect http to https in .htaccess file
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example/$1 [R=301,L]
It redirects all request on port 80 to https.