Reputation: 11
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?
Upvotes: 1
Views: 7235
Reputation: 19
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;
}
Upvotes: 2
Reputation: 83
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;
}
?>
Upvotes: 2
Reputation: 11
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.
Upvotes: 0
Reputation: 24151
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;
Upvotes: 0
Reputation: 36681
if( isset($_SERVER['HTTPS'] ) ) {
header( "Location:foo.php");
Upvotes: 1