Redirect browser to https in php

223 views
asked May 11, 2012 in SSL by chris (500 points)
hello everyone

I have an ecommerce website that uses payment gateway for online payments. I have SSL installed, and I want to redirect the browser to https instead of http.
Can someone here please tell me how to do this in php?
Thanks

2 Answers

0 like 0 dislike
answered May 11, 2012 by expert (14,380 points)
Function to redirect the browser to 'https'in PHP
function redirectToHTTPS()
{
  if($_SERVER['HTTPS']!="on")
  {
     $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     header("Location:$redirect");
  }
}
0 like 0 dislike
answered May 11, 2012 by WebGuru (9,280 points)
if you want to redirecting the whole website to 'https' it's easier to do it using .htaccess
this is the code tou should use
  RewriteEngine On
  RewriteCond %{HTTPS} !on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
...