Categories
asp .net PHP

3 Simple Ways To Redirect a Website From Http to Https

Method:1- .htaccess file

You can use this code and edit .htaccess file to move from Http to Https,

Please add this to the .htaccess file

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

 Generally websites use the http protocol as a default protocol to handle all the information, but Https can be a secure way to run a website online.

How it Works?

The three commands: RewriteEngine, RewriteCond and RewriteRule.

The “RewriteEngine On” tells Apache we are going to use mod_rewrite. The “RewriteCond % {HTTPS}” off, to check if the https protocol is active or not, and If the https protocol is used then the last line (RewriteRule) will not apply. The last line “RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}” tell the server to only rewrite the first part (http://) to (https://).

Method:2 – Using php Function

You can use the following php function to redirect your website using php code. You just have to call the function in the page at appropriate place 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”); } }

?>

How it Works?

Please check first that SSL is installed on the server. Now to redirect the browser to “https” , You must know that the site is using SSL or not at the moment. And for this, there is a defined server variable in PHP called “HTTPS”. $_SERVER[‘HTTPS’] returns “on” values when the site is using an SSL connection.

 Method :3 – HTML Meta Tag

Above two methods are enough to make a move from Http to Https, in comparison 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 web page header,

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

How it Works?

Please make sure the site with SSL encryption is listening only on port 443 for the IP address you’ve assigned. Now create a separate website using similar IP address, which pay attention to port 80. Create a single file at the root level and call it as default.htm or default.asp. Use meta refresh tag, if you want to use HTML.