Difference between revisions of "How To/HTTP to HTTPS redirect"

From Roaring Penguin
Jump to: navigation, search
(Ticket #38831)
Line 8: Line 8:
 
   # Redirect HTTP => HTTPS
 
   # Redirect HTTP => HTTPS
 
   if (!array_key_exists('HTTPS', $_SERVER) || !$_SERVER['HTTPS']) {
 
   if (!array_key_exists('HTTPS', $_SERVER) || !$_SERVER['HTTPS']) {
       header("Location: https://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
+
       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();
 
       exit();
 
   }
 
   }

Revision as of 15:43, 21 January 2015

You can do that 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 ?>