What are some common methods to enforce HTTPS using PHP when access to httpd.conf is restricted?

When access to httpd.conf is restricted, one common method to enforce HTTPS using PHP is to check if the current request is not secure and then redirect the user to the secure version of the URL. This can be done by checking the value of the $_SERVER['HTTPS'] variable and the $_SERVER['HTTP_HOST'] variable to construct the secure URL.

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