How to redirect page to https in php?

Member

by lottie , in category: Third Party Scripts , 5 months ago

How to redirect page to https in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 2 months ago

@lottie 

To redirect a page to HTTPS in PHP, you can use the following code snippet:

1
2
3
4
5
6
7
// Check if the current request is not secure
if ($_SERVER['HTTPS'] != 'on') {
    // Redirect to the same page with HTTPS
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}


You can add this code at the beginning of your PHP file to automatically redirect the page to HTTPS if it is accessed over HTTP. This will ensure that your website is always served securely over HTTPS.