PHP, WHM - Plesk - CWP

PHP SSL Redirect Code

Using the php ssl redirect code below, you can redirect all visitors and search engine bots on your SSL-installed site to https. You can use it as an alternative on servers where the htaccess ssl redirect code does not work.

PHP SSL Redirect Code

$oguz = (@$_SERVER["HTTPS"] == "on") ? "https://" : "https://";
/* If the server's Apache version is not compatible or https control is not working, we redirect to https in all cases. */
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
header('Location: '.$oguz.'www.'.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI']);
exit;
}

Alternative PHP SSL Redirect Code

If the above php ssl redirect code does not work, you can try absolute redirect using the following alternative redirect code.

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

You can add the codes to your index.php file in the main directory of your site.

Leave a Comment

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

Scroll to Top