As we know that all transactions over internet uses SSL (secure socket layer) connection to transfer data to and from the payment gateway. commonly most of websites uses “HTTP” protocol but When we need to integrate payment gateways on e-commerce site or any other. Then we need to redirect the browser from common “HTTP” to Secure “HTTPS” which mean that “Hypertext Transfer Protocol over Secure Socket Layer”.
We can see the real example of HTTPS in Banks, reservation sites when we type “HTTP” it automatically converts to “HTTPS” in address bar which means this site is transferring the data over SSL protocal.
Redirect the browser to https when site is using http protocal in PHP
First of all, you will have to configure SSL on your server. After we will check that the site is using SSL or not by PHP server varriable called $_SERVER['HTTPS'] which return “ON” valuewhen the site is using SSL Connection.
PHP Function to redirect the browser to “https”
function redirectToSecureHTTPS()
{
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
}
{
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
}
This is a very simple php fucntion, you can call this function in the page where you’ve to redirect the browser to “https”.
Redirect the browser to “https” using .htaccess
The above php function need to be call on each page to redirect the browser to “https”. But by using .htaccess file, you can redirect the whole website to use SSL connection throughout the pages.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You just need to copy and paste the above code in .htaccess file and the whole website will be redirected to “https”. The browser will be et redirected using url rewriting in .htaccess.
0 comments:
Post a Comment