redirect php request to secure ssl host

In the following example we will direct to a specific host. This can prevent trouble with indexing your site by spiders. Then there is only one host. And any requests will be routed to your choosen host.

<?php

// $server_redirect is the hostname you want to redirect to
// e.g. www.example.com
$server_redirect = get_main_ini('server_redirect');
    if ($server_redirect){
        if($_SERVER['SERVER_NAME'] != $server_redirect){
            if ($_SERVER['SERVER_PORT'] == 80) {
                $scheme = "http://";
            } else {
                $scheme = "https://";
            }

            $redirect = $scheme . $server_redirect . $_SERVER['REQUEST_URI'];
            header("Location: $redirect");
        }
    }

    // substitue get_main_ini('server_force_ssl') with e.g. true. 
    if (get_main_ini('server_force_ssl')) {
        if ($_SERVER['SERVER_PORT'] != 443){
            $redirect = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
            header("Location: $redirect");
        }
    }
?>

This page has been viewed 2947 times. First hit: 30-Oct-2015 13:52:08
Share email, Google+, Twitter, Facebook.
comments powered by Disqus
Table