Redirect http to https (SSL for entire website)

0
15

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. You may know it from website URLs like: “http://www.google.com”

Difference between http and https:

First of all http stands for “Hyper Text Transfer Protocol” and The “S” in https stands for “Secure Socket Layer” (SSL). Http is the protocol that currently is being used by almost all websites worldwide. The https is another protocol primarily developed with secure, safe Internet transactions in mind. The https makes sure your information is send in a secure way to and from the server.

Redirection option: .htaccess

You can use the https protocol if you have a SSL certificate and have installed it correctly. Most websites use the http protocol as a default protocol to handle all the information. You can force your website to use the https protocol by creating or modifying an “.htaccess” file in the folder (e.g. root) where you want the redirect to happen.Please add this to the .htaccess file

RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

ExplanationWe are using three command in the code above: RewriteEngine, RewriteCond and RewriteRule.The “RewriteEngine On” tells Apache we are going to use mod_rewrite. The “RewriteCond %{HTTPS}” off, check if the the https protocol already in use. If the https protocol is use then the last line (RewriteRule) wont apply. The last line “RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}” tell the server to only rewrite the first part (http://) to (https://).

Redirection option: Using php

You can use the following code (function) to redirect your website using php. you can call the function in the page where you need the redirect from http to https.

 

< ? php
function redirectTohttps() {
    if ($_SERVER['HTTPS'] != ”on”) {
        $redirect = “https: //”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];header(“Location:$redirect”); } } ?>

First of all, you should know that SSL must be installed in the server. To redirect the browser to “https” , we must know that the site is using SSL or not at the moment. And for this, there is a server variable in PHP called “HTTPS”. $_SERVER[‘HTTPS’] returns “on” values when the site is using SSL connection.Source: phpgenious.com

Redirection option: HTML meta tag

This method is not the most perfect one but you can use it if you are not able to use the “mod rewrite”.Please add the following code to your header

< meta http-equiv="Refresh" content="0;URL=https://www.example.com" />

Make sure the original site (the one with SSL encryption) is listening only on port 443 for the IP address you’ve assigned to it. Now create a separate site using that same IP address, and make sure it only listens on port 80. Create a single file at the root level and call it default.htm or default.asp. If you want to use HTML, then use a meta refresh tag.

LEAVE A REPLY

Please enter your comment!
Please enter your name here