Redirecting HTTP to HTTPS

Written on 18/05/2020

Redirecting HTTP to HTTPS

Redirecting HTTP to HTTPS can be achieved in many ways, the easiest way is to edit the .htaccess file. Other two popular ways are to use script or meta tag.

Editing .htaccess File

1. Redirect All Web Traffic

If you have existing code in your .htaccess, add the following:

1
2
3
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

 

2. Redirect Only a Specific Domain

For redirecting a specific domain to use HTTPS, add the following:

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

3. Redirect Only a Specific Folder

Redirecting to HTTPS on a specific folder, add the following:

1
2
3
4
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]

Note: Replace “yourdomain” with your actual domain name wherever required. Also, in case of the folder, replace /folder with the actual folder name.

Resource

https://serverguy.com/servers/redirect-http-to-https/

More Ways

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.

Resource

https://www.eukhost.com/kb/3-simple-ways-to-redirect-a-website-from-http-to-https/