Redirect a site to HTTPS using PHP

This PHP script will redirect web site visitor to HTTPS (SSL) url. You can add this in your index.php of the web site

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ) {
    header("HTTP/1.1 301 Moved Permanently");
    $newUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    Header("Location: $newUrl");
    exit;
}

Move a site to new URL using PHP

$newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently");
Header("Location: $newUrl");
exit;

You can also use Apache mod_rewrite .htacess to do the redirection.

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *