How to Redirect HTTP to HTTPS Using .htaccess?

What is HTTP to HTTPS Redirect?

HTTP-to-HTTPS redirection is a process to redirect non-secure web pages to secure web pages. This is a trust signal for users who are willing to buy something from the website.

By early 2018, Google started flagging potentially unsafe websites on its search engines. The result?

If statistics are anything to go by, nearly 66% of all websites have started displaying the message ‘Not secure’ in the URL bar. Therefore redirecting HTTP to HTTPS is becoming mandatory.

However, these unsafe websites were using ‘HTTP,’ which didn’t provide data encryption. The move was part of a plan by Google to force websites to adopt more secure ways to transfer data, namely ‘HTTPS.’ That’s why you need to buy an SSL certificate and start using ‘HTTPS’ as soon as possible, to avoid being negatively classified by Google. You can use .htaccess to redirect your site on HTTPS.

Related Read: HTTP vs HTTPS.

cheap ssl certificate

What is .htaccess?

Simply put, it is a configuration file. If you want to migrate to ‘https’ from ‘http’, you need to make a few changes in your .htaccess file. A .htaccess file comes with necessary instructions that can help you do that.

They are called directives, and two of the most common are:

  • Redirects
  • Rewriting URLs

Therefore, .htaccess offers a way to make changes in the configurations on a per-directory basis.

How to edit a .htaccess file?

Editing can be done in four simple ways. You can choose any one of them based on your requirements.

Here are some of the ways to edit your .htaccess file:

Step 1: Access and edit the computer file and upload it to the server using File Transfer Protocol

Step 2: Click on the ‘edit mode’ in the File Transfer Protocol. It allows you to edit files remotely

Step 3: You can edit the file via text editors or Secure Shell Protocol

Step 4: Go to cPanel >> ‘File Manager’ and edit the file

Editing .htaccess in cPanel File Manager

Step 1: First, login to cPanel

Step 2: Browse to File Manager > Document Root:

cpanel file manager

Step 3: Choose the domain name you want to gain access to

Step 4: Check “Show Hidden Files (dotfiles)”

cpanel show hidden files dotfiles

Step 5: Click on “Go”

Step 6: You will have a new window where you need to search for the .htaccess file.

Step 7: You need to right click on the .htaccess file then, click on “Code Edit” on the menu.

Step 8: There will be a dialogue box for encoding. Click on “Edit” button.

Step 9: Now, edit the file and click on “Save Changes”.

Step 10: Finally, click on “Close”.

How to Redirect HTTP to HTTPS Using .htaccess?

For ‘HTTP’ to ‘HTTPS’ redirect using .htaccess, you have the option of being selective about what you need and want. It lets you pick and choose. There are mainly 3 ways you do HTTPS migration.

1. Redirect All traffic Using .htaccess

To ensure ‘HTTPS’ on your entire traffic, you can use .htaccess and edit it. This command ensures that a URL is shifted permanently from one location to the other, and you are redirected to the new one. This option is the most widely used when you want to force HTTPS on all traffic.

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

2. Redirect Specific Domain Using .htaccess

In many cases, you don’t need to force HTTPS on all the domains that are there. Sometimes, you just need to enforce it upon a specific domain.

Suppose you have three domains, X, Y, and Z. But if you want to enforce ‘HTTPS’ on the Y domain only. For that, you don’t need to force it upon X and Z domains as well. A simple piece of code can help you out specifically in the case of Y:

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

Take care to insert this code after the line saying ‘RewriteEngineOn’. All you need to do is replace ‘domain’ in the first line of the code with the URL of your own domain. ‘HTTPS’ will be forced on immediately.

3. Redirect Specific folder Using .htaccess

You can also use the ‘.htaccess’ to force ‘HTTPS’ on specific folders, and not just the domains. For ‘.htaccess’ to work, it must first be kept in a folder that has the ‘HTTPS’ connection.

You can ensure HTTPS on a specific folder with the help of the following bit of code:

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

When you’re at it, take care to tweak this code to meet your needs. To use this, you’ll need to replace the ‘folder1, folder2 and folder3’ with the actual directory names being referred to.
After you’ve followed all these steps, you’re good to go.

The only thing that you need to check is your cache. Sometimes, the cache fills up, and it hinders your browser from working efficiently and incorporating the changes that you would want. Therefore, after performing the operations, you should always remember to clear the cache.

If you further want to test the results of your actions and the changes that you’ve made, you could deliberately go to the version of your website using ‘HTTP.’ If you’ve made all the right changes and saved your work properly, you should get redirected to the version of your website with ‘HTTPS.’

Winding-Up

So, that’s about it!

You can use .htaccess to redirect from HTTP to HTTPS from the above steps.

All this might seem like a long drawn out and time-consuming activity to you right now. But migrating from ‘HTTP’ to ‘HTTPS’ is like insurance against any future incident that might hamper the sanctity of your website.

Cybercrime is on the rise, and one can’t be too careful these days. When you use ‘HTTPS’, you are basically securing your website with an SSL certificate, which is a sign of trust and security.

It is a signal to your customers that you adhere to the highest security standards and have a fully encrypted data transfer protocol in place.

‘HTTPS’ not only helps you make your website more secure but reinforces the people’s trust in your website.

Related Post

 

We Assure to Serve

Leading Brands

Leading Brands

ClickSSL is platinum partner of leading CAs & offering broad range of SSL certificate products.

Valued Price

Valued Price

You are at right place to get cheapest SSLs; our prices are up to 79% low as compared to CAs.

100% Refund Policy

100% Refund Policy

If you are not satisfied, our all SSL certificates are backed by 30-day 100% money back guarantee.

24×7 Support

24×7 Support

Our experts are always active to help you, so you will get instant solutions for your queries.