How to redirect your website from HTTP to HTTPS?

How to redirect your website from HTTP to HTTPS?

April 3, 2021 | 1 min read

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];    header('HTTP/1.1 301 Moved Permanently');    header('Location: ' . $location);    exit;}<?phpif (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' ||    $_SERVER['HTTPS'] == 1) ||     isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&      $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')){   $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];   header('HTTP/1.1 301 Moved Permanently');   header('Location: ' . $redirect);   exit();}?>