Click here to Skip to main content
13,736,555 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 10 Nov 2017
Licenced CPOL

How to Redirect HTTP to HTTPS Automatically

, 10 Nov 2017
Rate this:
Please Sign up or sign in to vote.
If you want that your site always opens with SSL/ HTTPS only, then you need to redirect HTTP to HTTPS. There are several methods to do it. But in this tip, I share the easiest HTTPS redirection methods.

Sometimes, it is beneficial to redirect your users through SSL secure connection. If you are running an E-commerce website that collects sensitive information like credit card details, passwords, etc., then SSL is a must. On 6th August 2014, Google officially announced this as ranking signal. There are several reasons that you need to move your site to SSL/ HTTPS right away.

  • Google officially announced that “Now all the sites that are using SSL/ HTTPS will get a ranking boost on Google search results. This will be a huge boost, but however, HTTPS sites will rank higher than HTTP sites.
  • The main reason to use SSL is to keep the site secure. If any site collects important data, i.e., passwords, user details, credit card information, then those data can be hacked easily through an unsecured connection. But an SSL enabled site encrypts all the sensitive information and passes to the server. This way, all information remains safe.
  • Your customer trusts your site. Having an SSL on your site makes them sure that your website is protected and safe to provide sensitive data.

If you are running an E-commerce site, then you need a premium SSL which costs around $70. If you run a blog website or don’t collect credit card information, then you can easily use free SSL Certificate from Let’s Encrypt.

After installing SSL certificate, many times users have not been redirected to HTTPS automatically. So every time a user needs to type the full website address including HTTPS. For example, This is quite annoying as most of the users don’t enter a website by typing the full address on their web browser. In this case, you can force SSL or redirect HTTP to HTTPS, so all the users will be automatically redirected through a secure connection.

There are many ways in which you can redirect HTTP to HTTPS and I am sharing those easiest methods that you can easily force SSL on your site.

Redirect HTTP to HTTPS through cPanel

If your web hosting company provides free SSL certificate, then you can install it directly from your cPanel. To do this, log in to your cPanel and go to security and from there, install Let’s Encrypt SSL certificate. It is a very simple method and it can be installed with just one click.

Redirect HTTP to HTTPS using Apache mod_rewrite

If your web server runs by Apache, then you can force SSL or redirect SSL using a .htaccess file. Add the following code to your site .htaccess file.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want to force SSL on a specific folder of your site, then add the following code to your site .htaccess file.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} yourfolder
RewriteRule ^(.*)$$1 [R,L]

HTTPS Redirection in Nginx

If your web server is running Nginx, then you can easily redirect all HTTP traffic to HTTPS by adding the following code in your Nginx config file.

First, go to “/etc/nginx/nginx.conf” and add the following code:

server {
listen 80;
return 301$request_uri;

Don't forget to save it.

Using this method, your website will be accessible only SSL/ HTTPS connection. If you are getting an error or stuck on any step, please leave a comment below. I will be glad to help you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jyoti Ray
India India
Jyoti Prakash Ray is the founder of He writes about Blogging, WordPress tutorials, Hosting, Affiliate marketing etc. He mostly spends times on blogging, reading books and cooking.

You may also be interested in...

Comments and Discussions

PraiseIIS redirect Pin
obermd13-Nov-17 9:41
memberobermd13-Nov-17 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 10 Nov 2017
Article Copyright 2017 by Jyoti Ray
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid