.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Port} !=443
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

PHP

$SERVER_HTTPS = false; 
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){
  $SERVER_HTTPS = true;
}
if (! $SERVER_HTTPS) {
  header ("Location:
https://" . $_SERVER["SERVER_NAME"]);
  exit;
}

Javascript

if (location.protocol == "http:" || location.protocol == "http"){
  location.href = "https://" + location.host;
}
Vlastimil Pošmura, 2019