How To/HTTP to HTTPS redirect

From Roaring Penguin
Jump to: navigation, search

You can do this by making a file:

  /var/www/canit/site/config.d/0_redirect.php

with the following content;

  <?php
  # Redirect HTTP => HTTPS
  if (!array_key_exists('HTTPS', $_SERVER) || !$_SERVER['HTTPS']) {
     if (empty($_SERVER['QUERY_STRING'])) {
        header("Location: https://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
     } else {
        header("Location: https://" . $_SERVER['SERVER_NAME'] .
             $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
     }
     exit();
  }
  ?>

Note: nothing before <? or after ?>