Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Last active January 16, 2019 16:11
Show Gist options
  • Save kobus1998/fac0ceb772f09bd2aa26d3fd3515d3f8 to your computer and use it in GitHub Desktop.
Save kobus1998/fac0ceb772f09bd2aa26d3fd3515d3f8 to your computer and use it in GitHub Desktop.
Force https PHP
<?php
/**
* Force to redirect to https
*
* @return void
*/
function forceHttps()
{
// check already https
if (isset($_SERVER['https']) && $_SERVER['https'] != null) return;
// check local
if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false) return;
header("HTTP/1.1 301 Moved Permanently");
header("Reason: HTTPS");
header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
die;
};
forceHttps();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment