简体   繁体   中英

How to redirect a website from HTTP to HTTPS without using URL Rewrite?

I have tried it using URL Rewrite it works fine, but i wanted to know if there is another best solution

<rewrite>
        <rules>
            <rule name="HTTP to HTTPS " enabled="true" stop Processing="true">
                <match URL="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" URL="https://{HTTP_HOST}/{R:1}" />
            </rule> 
        </rules>

This is the code i have used for redirection

I use PHP code to handle the redirection as below and should work on any server with PHP and SSL. You do have to specify what subdomains you will be using but that is minor and usually doesn't change much. Just place this in a file and include it at the top of any page you want redirected to HTTPS.

if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || 
   $_SERVER['HTTPS'] == 1) ||  
   isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&   
   $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
    //echo $_SERVER["HTTP_HOST"];
    $subdomains = array('cp','rec','dsm','outback','live','pw');
    $host = explode('.',$_SERVER['HTTP_HOST']);
    if(in_array($host[0],$subdomains)) {
       $redirect = 'https://' . str_replace('www.','',$_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
    }
    else {
       $redirect = 'https://www.' . str_replace('www.','',$_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
    }
    echo $redirect;
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   exit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM