How to configure it.htaccess for automatic site translation to https?

Added an SSL certificate to the site. Now you need to have any access to the site via http: automatically translated to https:

Tell me how you need to change this one .htaccess to get the desired behavior:

AddDefaultCharset UTF-8
ErrorDocument 404 /index.php?page=404

RewriteEngine On

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

RewriteBase /    

# RewriteRule ^index.php$ / [R=301,L]

RewriteRule ^test/(x.+/)?$ index.php?page=test [L]
RewriteRule ^test/create/((de|es)/)?$ index.php?page=create&lang=$2 [L]
RewriteRule ^test/create/(([0-9A-Fa-f]+)/)?$ index.php?page=create&token=$2 [L]
RewriteRule ^test/([^/]+)/$ index.php?page=test&act=$1 [L]
RewriteRule ^test/replay/([0-9A-Za-z]+)/$ index.php?page=test&replay=$1 [L]

Line change only:

RewriteRule ^(.*)$ http://www.somedomain.ru/$1 [R=301,L]

On:

RewriteRule ^(.*)$ https://www.somedomain.ru/$1 [R=301,L]

Does not give the desired behavior in all cases.

UPD: updated .htaccess to option 9 from @Alex's response:

AddDefaultCharset UTF-8
ErrorDocument 404 /index.php?page=404

RewriteEngine On

# https://masterhost.ru/support/doc/apache/#http2https

RewriteCond %{HTTP:PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# https://masterhost.ru/support/doc/apache/#redirect

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

# старый вариант переадресации на www.
#RewriteCond %{HTTP_HOST} ^somedomain\.ru
#RewriteRule ^(.*)$ http://www.somedomain.ru/$1 [R=301,L]

RewriteBase /

# RewriteRule ^index.php$ / [R=301,L]

RewriteRule ^test/(x.+/)?$ index.php?page=test [L]
RewriteRule ^test/create/((de|es)/)?$ index.php?page=create&lang=$2 [L]
RewriteRule ^test/create/(([0-9A-Fa-f]+)/)?$ index.php?page=create&token=$2 [L]
RewriteRule ^test/([^/]+)/$ index.php?page=test&act=$1 [L]
RewriteRule ^test/replay/([0-9A-Za-z]+)/$ index.php?page=test&replay=$1 [L]

... works.

Author: xhr, 2019-01-09

2 answers

It all depends on the settings of the hoster, so first of all I recommend to look at the faq of the hoster, it will save you a lot of questions and save a lot of time.

I'll give you a few options below. It is advisable to write immediately after the directive

RewriteEngine On

Option 1

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

Option 2

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Option 3

RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

Option 4

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

Option 5

RewriteCond %{HTTP:X-HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Option 6

RewriteCond %{HTTPS} =off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]

Option 7

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Option 8

RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteRule ^(.*) https://www.сайт.com/$1 [L,R=301]

Option 9

RewriteCond %{HTTP:PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
 2
Author: Alex, 2019-01-11 21:35:38
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^somedomain.ru$ [NC]
RewriteRule ^(.*)$ https://www.somedomain.ru/$1 [L,R=301]
 0
Author: Andrew Hobbit, 2019-01-09 19:13:01