<?php
// Check if the connection is NOT HTTPS
if (!(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'];
    // Issue a 301 Moved Permanently header for SEO best practices
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $redirect);
    exit(); // Stop further script execution
}
?>
