开发者

Redirect HTTPS to HTTP

Very simple issue we cannot resolve. The following code does not redirect properly:

$location = str_replace("http","https",$this->page->current_url);
header( "Location: ".$location );

I get a 400 Error: "Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port.Instead use the HTTPS scheme to access this URL, please."

How can I redirect in PHP 开发者_运维百科from a page served over HTTPS to a page served over HTTP for scenarios such as secure login and then redirect to nonsecure app page?


try this, for me works fine

<?php
if  ( $_SERVER['HTTPS'] )
    {
            $host = $_SERVER['HTTP_HOST'];
            $request_uri = $_SERVER['REQUEST_URI'];
            $good_url = "http://" . $host . $request_uri;

            header( "HTTP/1.1 301 Moved Permanently" );
            header( "Location: $good_url" );
            exit;
    }
?> 


This will work for http to https and https to http (use some tweaks to check)

First check whether the https is ON on your hosting if yes than redirect the URL to https using below mentioned function in PHP

function httpToHttps() 
{
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") 
    {


    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

    } 
    else 
    {

    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_UR

I"];
    }
    return $pageURL;
}


    if( isset($_SERVER['HTTPS'] ) ) {
    header( "Location:foo.php");


Not sure if this can cause such a problem, but you should add an exit after calling the header function. This makes sure that the following code will not be executed and the HTTPS request itself returns no data. The header function should be called before anything is returned.

$target = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('Location: '.$target, true, 303);
exit;


Duh - I have no clue why I did not look at $location. It contained http//localhost:443/[...] which is invalid. I simply remove the :443 and it works fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜