How to Redirect HTTP to HTTPS Complete Guide using Nginx, Apache, htaccess

Robin
Updated on April 3, 2022

When you want to redirect HTTP to HTTPS, you have so many options according to your situation. There are many ways to do this:

  • Redirect HTTP to HTTPS in Nginx.
  • Redirect HTTP to HTTPS in Apache.
  • Redirect HTTP to HTTPS in WordPress.
  • Redirect HTTP to HTTPS using htaccess in cPanel.

In this article, I will show you all these ways to redirect a website from HTTP to HTTPS step by step so that you can choose according to your requirements.

Requirements for HTTPS

First, you need to know the least requirements to follow this tutorial. These are the things you must have to use HTTPS and redirect your website from HTTP to HTTPS.

  • A domain name.
  • A web hosting, it might be Shared hosting or VPS hosting.
  • Install SSL certificate.

You can purchase a domain name from any domain provider like Namecheap, or Hover. They provide domain names at a cheap price.

Then you must have a web hosting for your website. I prefer VPS hosting over shared hosting. Because VPS hosting is fast and reliable. You can customize them according to your website's needs.

Finally, you need an SSL certificate that actually provides the HTTPS protocol for a website. You can install an SSL certificate for free using Let's encrypt.

Also Read: Synchronous VS Asynchronous Programming with Examples

What is an SSL Certificate?

The SSL (Secure Sockets Layer) is a standard security protocol to establish secure communication between a web server and a web browser.

This technology ensures that the data remains encrypted when a web server and browser share between them so that hackers can not steal.

An SSL certificate is essential to create an HTTPS connection. You can purchase this certificate if you need very strong security for your website.

Otherwise, you can use the free let's encrypt certificate. It provides standard security encryption. Most of the hosting providers also give free certificates.

Why do You Need HTTPS?

HTTPS has become the standard protocol for any type of website. Google has added this as their ranking factor.

When someone browses a website in Chrome, the browser shows the "not secure" flag and tries to prevent users to visit that website.

There are some primary seasons to use the HTTPS protocol on a website using an SSL certificate.

  • Better security - it ensures data encryption in the network while sharing between a server and a web browser.
  • Google Search Engine Ranking - having an SSL certificate is a ranking factor in Google. Search engine gives priority to the websites that use HTTPS protocol over HTTP.
  • Google Chrome Browser - it shows a secure green flag to those websites that have an SSL certificate installed. Otherwise, it discourages users to visit those websites.
  • Increase User's Trust - in recent years, people are getting more concerned about their security. When users see the green secure sign in their browser, they intend to trust that website more.

When you redirect HTTP to HTTPS

When your website meets all the requirements mentioned above like having a domain name and web hosting then you can go to the redirection process.

Sometimes having an SSL certificate installed might not be enough. Because it automatically does not redirect from a non-secure version to a secure version.

If you install a certificate using Let's encrypt then it will ask you to set up auto-redirect. If you say "Yes", it will redirect HTTP to HTTPS automatically on your behalf.

Otherwise, it is the time you configure the settings so that users can use the HTTPS version of your website.

I will show you exactly how you can do that very easily step by step.

How to redirect HTTP to HTTPS using htaccess in cPanel

We are going to redirect HTTP to HTTPS using our .htaccess file. You have to add some code inside your .htaccess file for that.

However, don't worry I will provide all the code necessary to complete this process. You can copy and use them.

We will complete this whole process in 3 easy steps. These steps are:

  • Login to your cPanel.
  • Open .htaccess file there.
  • Copy and paste code in .htaccess file.

Step 1: Login to your cPanel

First, enter your domain name with /cpanel at the end. For example, http://example.com/cpanel

Enter all the necessary credentials like username and password for your cPanel and login.

Because you are going to use .htaccess to redirect HTTP to HTTPS. Find your “File Manager” option and go inside.

How to redirect http to https using htaccess in cpanel

Step 2: Open .htaccess file there

Now click on the “public_html” folder. If your website is installed in the root folder then you will find all the files inside this public_html folder.

Otherwise, go to the specific folder inside public_html where your website is installed.

How to redirect http to https using htaccess in cpanel

Now find out the “.htaccess” file in this folder. If you don’t find it that’s ok. Click on “Settings” at the top right corner.

This will open up a new popup. Here you have to select “Show Hidden Files(dotfiles)” and click on "Save".

How to redirect http to https using htaccess in cpanel

Now probably you will find the .htaccess file if you have one. If it does not show up then create a .htaccess file by clicking the “File” option at the top left corner.

Right-click on your .htaccess file then click “Edit”. This will open up a new popup. In this popup, you have to click “Edit” again. Now you will get your .htaccess file opened on a new tab.

How to redirect http to https using htaccess in cpanel

Step 3: Copy and paste code in .htaccess file

I believe your .htaccess file is opened on your browser. So it is time to write some code. You have to copy and paste 3 lines of code in this file to redirect HTTP to HTTPS.

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

Just copy these 3 lines of code and paste them at the very top of your .htaccess file. In addition, don’t forget to save your .htaccess file after doing all these changes.

Now click on the “Save Changes” button at the top left corner to save your .htaccess file.

Now type your domain name in your browser without using HTTPS at the beginning. I believe it will redirect to HTTPS automatically.

How to Redirect HTTP to HTTPS in Nginx

Nginx is a very popular web server. If your website is running on Nginx, this method will work for you.

To redirect your website from HTTP to HTTPS in Nginx, add the following code in your Nginx configuration file.

First, log in to your web server. Usually, you will find the configuration file in the /etc/nginx/sites-available folder.

Inside that folder, you will find a file named after your website domain name like example.com.conf

If the file is present, you are good to go. But if it is not available, that means your website is using the default config file that is default.conf. You will find that file inside the same folder.

It does not matter which config file you are using. Just open that file and add the following code to that.

          server {
    listen          80;
    listen          [::]:80;
    server_name     example.com www.example.com;
    return 301      https://example.com$request_uri;
}
        

The above section will run in your config file if someone visits the HTTP version of your website. It is listening to port 80 which is the default port for HTTP.

Just change the domain name from example.com to yours in lines 4 and 5.

In line 5, $request_uri represents the path user trying to visit. If someone tries to visit http://example.com/about, here $request_uri contains the value /about.

It means, whatever page a user is trying to visit, this code will create a 301 redirects to the same page in HTTPS protocol.

Now save your configuration file.

In your terminal, run this command sudo nginx -t. This command will check your config file. If it returns no error that means your configuration file is ready to work.

          sudo systemctl restart nginx
        

Note: These commands works on Ubuntu.

Run the above command in your terminal to restart your Nginx. You can check your Nginx status to confirm that your server is running using the next command. It will show active (running) if your Nginx server is running.

          sudo systemctl status nginx
        

Now visit your website, it will redirect all requests to the HTTPS version.

How to Redirect HTTPS to HTTPS in Apache

If your website is running on an apache server then follow this method. I will redirect requests to HTTPS using a virtual host.

For Ubuntu, it is a similar process. You have to find the apache config file and add some code.

But you need to have mod_rewrite module enabled in your system.

In Ubuntu, you can enable this module by running the following command.

          sudo a2enmod rewrite
        

For CentOS/RHEL users, add the following line in the httpd.conf file.

          LoadModule rewrite_module modules/mod_rewrite.so
        

You will find your config file in /etc/apache2/sites-available directory. Add the following code block in your config file.

          <VirtualHost *:80> 
    ServerName example.com
    ServerAlias www.example.com

    RewriteEngine On 
    RewriteCond %{HTTPS}  !=on 
    RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
        

In lines 2 and 3, replace example.com with your domain name. Then save the config file.

Make sure you restart or reload the apache service for changes to take effect.

For Debian and Ubuntu:

          sudo systemctl reload apache2
        

For CentOS and Fedora:

          sudo systemctl reload httpd
        

Now your website will redirect all requests to the HTTPS version.

How to Redirect HTTP to HTTPS in WordPress

In WordPress, you can install an SSL certificate and redirect HTTP to HTTPS just using a plugin. You don't have to write a single line of code.

You have to install Really Simple SSL plugin in your WordPress. To install this plugin go to "Plugins >> Add New" then type "Really Simple SSL" in the search bar.

Click on the "Install Now" then "Activate" buttons. After successful installation of the plugin, you will get a new page under "Settings >> SSL".

When you visit the page, you will find a button to install an SSL certificate for your WordPress website. Click the button and it will install the certificate as well as redirect HTTP to HTTPS automatically.

Conclusion

In this article, I have shown you several ways to redirect HTTP to HTTPS. Now you know how you can do this in Nginx, Apache, WordPress, or by using htaccess file in cPanel.

An SSL certificate not only provides security but also develops trust and brand value for the users. Now it also helps in Google search ranking.

Related Posts