Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - header location redirect: https to https, http to http

How do I properly use header function, so

header("location: http://".$_SERVER['HTTP_HOST']."/?para=abc"); //for http

and

header("location: https://".$_SERVER['HTTP_HOST']."/?para=abc"); //for https

could be written in 1 string if possible?

.htaccess file will take care of all http pages to be redirected to https with no problem, but I believe it makes sense to use a proper syntax for http/https pages in header("location:...), so it is correct for all browsers.

like image 516
Haradzieniec Avatar asked Sep 22 '26 04:09

Haradzieniec


2 Answers

You could also use the following code:

header("Location: //www.google.com");
like image 51
user1756209 Avatar answered Sep 24 '26 21:09

user1756209


$protocol='http';
if (isset($_SERVER['HTTPS']))
  if (strtoupper($_SERVER['HTTPS'])=='ON')
    $protocol='https';

header("location: $protocol://".$_SERVER['HTTP_HOST']."/?para=abc");
like image 42
Eugen Rieck Avatar answered Sep 24 '26 23:09

Eugen Rieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!