ScAlAn

Howto make redirect http to https on your php site

Task: Make redirect from http to https on your php site

Implementation:

Add this code at the top of your index.php file:

if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  //$protocol = 'https://';
  // Ok.
}
else {
  // $protocol = 'http://';
  // Should redirect.
  header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  exit;
}

Note: Tested with WordPress.

Done.

Leave a Reply

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

  +  17  =  24