Redirect HTTP to HTTPS Print

  • https, redirect to https, .htaccess, https protocol, free ssl, comodo ssl, shared ssl, use ssl, secure socket layer
  • 2

cPanel/Whm - Redirect HTTP to HTTPS

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. You may know it from website url’s 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

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 On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Explanation :
We 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://).

If you have an existing .htaccess file:

Do not duplicate RewriteEngine On.
Make sure the lines beginning RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.


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.

 

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.

 

Plesk - Redirect HTTP to HTTPS

Using the following code in your web.config file automatically redirects visitors to the HTTPS version of your site:

<configuration>
<system.webServer>
<rewrite>
    <rules>
	<rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
	<match url="(.*)" /> 
	<conditions> 
		<add input="{HTTPS}" pattern="off" ignoreCase="true" />
	</conditions> 
	<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>   
    </rules>
</rewrite>
</system.webServer>
</configuration>

If you have an existing web.config file:

Ensure you have sections (i.e. opening and closing tags) for:
system.webServer (which contains rewrite)
rewrite (which contains rules)
rules (which contains one or more rule sections)
Insert any of those sections that do not exist.
Insert the entire rule section, including match, conditions, and action, inside the rules section.
You're inserting the rule (without an 's') inside the rules (with an 's') section.


Was this answer helpful?

« Back

Powered by WHMCompleteSolution