What potential pitfalls should be considered when redirecting from HTTP to HTTPS in PHP?

When redirecting from HTTP to HTTPS in PHP, potential pitfalls to consider include ensuring that the redirect is done securely and efficiently to avoid any security vulnerabilities or performance issues. It's important to properly handle any existing query parameters or POST data during the redirection process to ensure a seamless transition for users.

// Check if the current request is not secure (HTTP)
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    // Redirect to the HTTPS version of the current URL
    header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}