What is the potential issue with using the PHP header() function to redirect from HTTP to HTTPS?

The potential issue with using the PHP header() function to redirect from HTTP to HTTPS is that it may not work correctly if the server configuration is not set up to handle HTTPS requests. To solve this issue, you can check if the current request is not already using HTTPS before redirecting.

if($_SERVER['HTTPS'] !== 'on'){
    header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}