Skip to content

Instantly share code, notes, and snippets.

@mostafizpantheon
Forked from csemrm/settings.php
Created April 20, 2019 11:33
Show Gist options
  • Save mostafizpantheon/fd8528d0c50e5b570e25d10e2ad339ea to your computer and use it in GitHub Desktop.
Save mostafizpantheon/fd8528d0c50e5b570e25d10e2ad339ea to your computer and use it in GitHub Desktop.
From bare domain to www for D8
<?php
########
if (isset($_ENV['PANTHEON_ENVIRONMENT']) && php_sapi_name() != 'cli') {
// Redirect to https://$primary_domain in the Live environment
if ($_ENV['PANTHEON_ENVIRONMENT'] === 'live' && ($_SERVER['HTTP_HOST'] !== 'live-psea.pantheonsite.io')) {
if ($is_www = strpos($_SERVER['HTTP_HOST'], 'www')) {
$primary_domain = $_SERVER['HTTP_HOST'];
} else
$primary_domain = 'www' . $_SERVER['HTTP_HOST'];
/** Replace www.example.com with your registered domain name */
} else {
// Redirect to HTTPS on every Pantheon environment.
$primary_domain = $_SERVER['HTTP_HOST'];
}
if ($_SERVER['HTTP_HOST'] != $primary_domain || !isset($_SERVER['HTTP_USER_AGENT_HTTPS']) || $_SERVER['HTTP_USER_AGENT_HTTPS'] != 'ON') {
# Name transaction "redirect" in New Relic for improved reporting (optional)
if (extension_loaded('newrelic')) {
newrelic_name_transaction("redirect");
}
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://' . $primary_domain . $_SERVER['REQUEST_URI']);
exit();
}
// Drupal 8 Trusted Host Settings
if (is_array($settings)) {
$settings['trusted_host_patterns'] = array('^' . preg_quote($primary_domain) . '$');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment