Skip to content

Instantly share code, notes, and snippets.

@gonssal
Created January 5, 2019 19:18
Show Gist options
  • Save gonssal/ea48be8f9815f257e5ed9e0c097a8f91 to your computer and use it in GitHub Desktop.
Save gonssal/ea48be8f9815f257e5ed9e0c097a8f91 to your computer and use it in GitHub Desktop.
PHP force HTTPS and canonical host URL
/**
* Force HTTPS and canonical host.
*/
if (php_sapi_name() != 'cli') {
$primary_domain = 'www.example.com';
if ($_SERVER['HTTP_HOST'] != $primary_domain || !array_key_exists('HTTPS', $_SERVER)) {
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://'. $primary_domain . $_SERVER['REQUEST_URI']);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment