2

Currently hosting a site on Pantheon and I would like to redirect traffic to a single domain. There is already a lot of links inside my site that are using:

http://example.com/somepage, http://www.example.com/somepage, https://example.com/somepage, https://www.example.com/somepage

Since Pantheon uses nginx I would have to use settings.php to redirect traffic. My Question is would it be possible to direct traffic to a single domain but it can be served in either http or https?

Some sample code:

// Require www.
if (isset($_SERVER['PANTHEON_ENVIRONMENT']) &&
  ($_SERVER['PANTHEON_ENVIRONMENT'] === 'live') &&
  (php_sapi_name() != "cli")) {
  if ($_SERVER['HTTP_HOST'] == 'example.com' ||
      $_SERVER['HTTP_HOST'] == 'https://example.com') {
    header('HTTP/1.0 301 Moved Permanently');
    //or https://www.example.com
    header('Location: http://www.example.com'. $_SERVER['REQUEST_URI']);
    exit();
  }
}
0
  • Nginx/settings.php

Try this: $base_url = 'https://'.$_SERVER['SERVER_NAME'];

  • Apache/.htaccess:

You can simply add the following to your .htaccess. It will redirect all traffic from http://example.com and http://www.example.com to https://example.com. Put that below RewriteEngine on.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example\.com*
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
| improve this answer | |
  • Pantheon use nginx, no .htaccess available. It has to be done in php – Clive Jan 12 '17 at 10:55
  • Hm ok, since the OP says "Since Pantheon doesn't use nginx". Thanks for clarifying. – Wim Mostrey Jan 12 '17 at 11:00
  • sorry that was a typo. i meant to write since pantheon uses nginx – blu Jan 12 '17 at 14:09
  • Np. I updated my answer to reflect this. – Wim Mostrey Jan 12 '17 at 14:11
0

If Drupal 7 Add the following to the end of your settings.php file (replace www.example.com):

if (isset($_SERVER['PANTHEON_ENVIRONMENT']) && php_sapi_name() != 'cli') {
  // Redirect to https://$primary_domain in the Live environment
  if ($_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
    /** Replace www.example.com with your registered domain name */
    $primary_domain = 'www.example.com';
  }
  else {
    // Redirect to HTTPS on every Pantheon environment.
    $primary_domain = $_SERVER['HTTP_HOST'];
  }

  if ($_SERVER['HTTP_HOST'] != $primary_domain
      || !isset($_SERVER['HTTP_X_SSL'])
      || $_SERVER['HTTP_X_SSL'] != '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();
  }
}

If Drupal 8 Add the following to the end of your settings.php file (replace www.example.com):

if (isset($_SERVER['PANTHEON_ENVIRONMENT']) && php_sapi_name() != 'cli') {
  // Redirect to https://$primary_domain in the Live environment
  if ($_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
    /** Replace www.example.com with your registered domain name */
    $primary_domain = 'www.example.com';
  }
  else {
    // Redirect to HTTPS on every Pantheon environment.
    $primary_domain = $_SERVER['HTTP_HOST'];
  }

  if ($_SERVER['HTTP_HOST'] != $primary_domain
      || !isset($_SERVER['HTTP_X_SSL'])
      || $_SERVER['HTTP_X_SSL'] != '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) .'$');
  }
}

Here's [a link] [https://pantheon.io/docs/guides/launch/redirects/]1 Redirect for SEO and Security Contribute

Make sure your DNS is setup & And you see the new domain/ https all in one turned on from Pantheon https://pantheon.io/docs/domains/#redirect-to-https-and-the-primary-domain (You need to see this for the Fastly CDN to use HTTPS enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.