Skip to main content
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Source Link
blu
  • 458
  • 3
  • 15

How to redirect certain requests to use https://www.example.com in pantheon?

In my Drupal install I would like to direct most URL requests to use https://www.example.com and only a few pages to use http://www.example.com Since my site is hosted on pantheon i cannot use .htaccess and i need to rely on my settings.php file. I tried the following but it doesn't really seem to work.

$redirect_domain = 'www.example.com';
if (isset($_SERVER['PANTHEON_ENVIRONMENT']) && $_SERVER['PANTHEON_ENVIRONMENT'] == 'live') {
  $redirect_location = '';
  // Do not require HTTPS for specific pages.
  if (in_array($_SERVER['REQUEST_URI'], array('/node/524','/some/unsecure/iframe'))) {
    // Check if HTTPS is enabled.
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
      $redirect_location = 'http://' . $redirect_domain . $_SERVER['REQUEST_URI'];
    }
  }
  // Require HTTPS for everything else.
  else if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
    if($_SERVER['HTTP_HOST'] != 'www.example.com'){
      $redirect_location = 'https://www.example.com' . $_SERVER['REQUEST_URI'];
    }
    else{
      $redirect_location = 'https://' . $redirect_domain . $_SERVER['REQUEST_URI'];
    }
  }
  // Perform redirect.
  if ($redirect_location) {
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: ' . $redirect_location);
    exit;
  }
}

typing in example.com does correctly redirect to https://www.example.com but typing in https://example.com doesn't perform any redirect.