http to https redirect in php
0 6706
From the security point of view, it is very important for dynamic websites to contain 'https' rather than 'http'.
http stands for Hyper Text Transfer Protocol.
It is a stateless protocol which is used to provide the ability to communicate between web browsers and servers.
In 'https' s stands for Secure Socket Layer or SSL in short.
It is an encryption based protocol which ensures privacy, authentication and data integrity in communication over the www.
We have 3 different ways to redirect a http page to https.
- By using PHP code
- By using HTML Meta tag
- By using .htaccess file
1) HTTP to HTTPS by using PHP code:
We can redirect a http page to https with the help some php code. Here is a brief code for it.
<?php
function redirect_httpTo_https() {
$header=$_SERVER['HTTPS'];
$http_host=$_SERVER["HTTP_HOST"];
$request_uri=$_SERVER["REQUEST_URI"];
if($header!="on") {
$redirect_url= "https://".$http_host.$request_uri;
echo "Redirect to https";
header("Location:".$redirect_url);
}
}
redirect_httpTo_https(); // call function to redirect
?>Output:

Related Article: A Guide to HTTP Status Code
2) http to https by using HTML Meta tag:
We can also use HTML meta tag to redirect a http page to https.
<meta http-equiv="Refresh" content="0;url=https://www.nameOfYourDomain.com"/>
Put this line into head tag. As compared to other two ways it is less effective.
3) http to https by using .htaccess file:
We know that htaccess file is very useful for url redirect. We can also redirect http to https by using htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Write above code into your htaccess file to make your website secure.
Share:




Comments
Waiting for your comments