Redirecting from HTTP to HTTPS with PHP

Force HTTPS

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

Usage

Call the function at the point you want to force https or on top of your global include file so to redirect on any page.


Comments

Add your comment

Need a COVID-19 website?
Preview
Or
Contact us

No, thanks!