Redirecting Users to HTTPS in PHP


Ensure that your site is accessed via HTTPS by redirecting HTTP requests.

Source Code

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;
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments