Created
January 5, 2019 19:18
-
-
Save gonssal/ea48be8f9815f257e5ed9e0c097a8f91 to your computer and use it in GitHub Desktop.
PHP force HTTPS and canonical host URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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