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']) &amp;&amp; $_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']) &amp;&amp; $_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.
That's because the $redirect_location doesn't get updated for https://example.com. See updated code below.


$domain = 'www.example.com';
if(isset($_SERVER['PANTHEON_ENVIRONMENT']) && $_SERVER['PANTHEON_ENVIRONMENT'] == 'live') {
  $location = '';

  if(in_array($_SERVER['REQUEST_URI'], array('/node/524'))) {
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
      $location = 'http://' . $domain . $_SERVER['REQUEST_URI'];
    }
  }
  else {
    if($_SERVER['HTTP_HOST'] != 'www.example.com') {
      $location = 'https://' . $domain . $_SERVER['REQUEST_URI'];
    }
    if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
      $location = 'https://' . $domain . $_SERVER['REQUEST_URI'];
    }
  }

  // Redirect
  if ($location) {
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
  }
}