Skip to content

Instantly share code, notes, and snippets.

@JonMcL
Last active September 14, 2017 16:17
Show Gist options
  • Save JonMcL/9f4b4acd31aee2207dfbeb9269de4081 to your computer and use it in GitHub Desktop.
Save JonMcL/9f4b4acd31aee2207dfbeb9269de4081 to your computer and use it in GitHub Desktop.
Redirect to www and https in Pantheon server environments. Add to Drupal 7 settings.php file.
/**
* Pantheon HTTPS and www redirects.
*/
if (isset($_SERVER['PANTHEON_ENVIRONMENT']) && php_sapi_name() !== 'cli') {
$domain = $_SERVER['HTTP_HOST'];
$www_redirect = false;
if (isset($_ENV['PANTHEON_ENVIRONMENT']) && $_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
if (count(explode('.', $domain)) < 3 && stripos($domain, 'www.') === false) {
$domain = 'www.' . $domain;
$www_redirect = true;
}
}
if ($www_redirect || !isset($_SERVER['HTTP_X_SSL']) || $_SERVER['HTTP_X_SSL'] != 'ON' ) {
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://'. $domain . $_SERVER['REQUEST_URI']);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment