Redirect HTTP to HTTPS Using .htaccess – How to Do?

Want to know How to redirect HTTP to HTTPS using .htaccess? We have shared all the information to make your job easy.

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 redirect 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.

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:

  • Access and edit the computer file and upload it to the server using File Transfer Protocol
  • Click on the ‘edit mode’ in the File Transfer Protocol. It allows you to edit files remotely
  • You can edit the file via text editors or Secure Shell Protocol
  • Go to cPanel >> ‘File Manager’ and edit the file

Editing .htaccess in cPanel File Manager

  • First, login to cPanel
  • Browse to File Manager > Document Root:
  • Choose the domain name you want to gain access to
  • Check “Show Hidden Files (dotfiles)”
  • Click on “Go”
  • You will have a new window where you need to search for the .htaccess file.
  • You need to right click on the .htaccess file then, click on “Code Edit” on the menu.
  • There will be a dialogue box for encoding. Click on “Edit” button.
  • Now, edit the file and click on “Save Changes”.
  • Finally, click on “Close”.

So how do you finally use the .htaccess for redirect http to https?

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 can do that.

1. Redirect All traffic Using .htaccess

To ensure ‘https’ on your entire traffic, you can use .htaccess and edit in 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 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 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 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 an insurance against any future incident that might hamper the sanctity of your website.

Cyber-crime 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.

 

We Assure to Serve

Leading Brands

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

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

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

24×7 Support

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