How to redirect page to https in php?

Member

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

How to redirect page to https in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 6 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.

Related Threads:

How to redirect to a php page?
How to redirect page after login in PHP?
How to redirect php page with .htaccess?
How to redirect after 5 seconds in PHP?
How to redirect http to https and https://www to https://?
How to redirect https://ip.address to https://domain?