http to https redirect in php || using .htaccess file

http to https redirect in php

0 2392

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.

  1. By using PHP code
  2. By using HTML Meta tag
  3. 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.

function redirect_httpTo_https() {
if($header!="on") {
$redirect_url= "https://".$http_host.$request_uri;
echo "Redirect to https";
redirect_httpTo_https(); // call function to redirect


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="/>

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.

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments