How to redirect HTTP to HTTPS Using .htaccess

https-redirects

Chrome and Firefox have been showing insecure website warnings on sites that do not have valid SSL certificates. Without valid SSL, the website will show insecurity to the visitors. Therefore, using a valid SSL-encrypted connection for safety is necessary. that is why It becomes very important to redirect your site from HTTP to HTTPS.

Read More:[wprpi]

What is SSL?

SSL (Secure Sockets Layer) is a standard security protocol for establishing encrypted links between a web server and a browser in online communication.

The usage of SSL technology ensures that all data transmitted between the web server and browser remains encrypted.

An SSL certificate is necessary to create an SSL connection. You would need to give all details about the identity of your website and your company as and when you choose to activate SSL on your web server. Following this, two cryptographic keys are created — a Private Key and a Public Key.

How to redirect HTTP to HTTPS

  1. Redirect All Web Traffic
  2. Redirect Only a Specific Domain
  3. Redirect Only a Specific Folder

How to

To force your web traffic to use HTTPS, there are two ways: to edit the codes in the .htaccess file.

Before we move on redirecting HTTP to HTTPS, here’s how you can edit the .htaccess file. you can skip to the Redirection steps if already know about it.

https valid ssl
Character illustration of people holding world wide web icons

Editing .htaccess File

There are instructions in the .htaccess file that tell the server how to act in certain scenarios which directly affects how your website will functions. Common directives in .htaccess file:

  • Redirects
  • Rewriting URLs

Ways to edit a .htaccess file:

  1. Edit the file on your computer and upload it to the server using FTP.
  2. Use “Edit” mode in FTP program that allows you to edit a file remotely.
  3. Use a text editor and SSH to edit the file.
  4. Use the File Manager in cPanel to edit the file.

Bonus:

Do you know in WordPress you can do this using a plugin, Here is a guide for you on how to install a WordPress plugin?

Editing .htaccess in cPanel File Manager

Note You should take a complete back of your site before any changes.

  1. Login to cPanel
  2. Files > File Manager > Document Root for:
  3. Now select the domain name you want to access
  4. Check “Show Hidden Files (dotfiles)”
  5. Click “Go”
  6. After a new tab or window opens, look for the .htaccess file.
  7. Right click on the .htaccess file and click on “Code Edit” on the menu.
  8. A dialogue box may pop up asking about encoding. Click “Edit” button to continue.
  9. Edit the file
  10. “Save Changes” when done.
  11. Test your website to make sure it is done correctly. In case, there is an error, restore to the previous version and try again.
  12. Once you are done, click “Close” to close the window.

1. Redirect All Web Traffic

If you have existing code in your .htaccess, then add the following code at the top

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:

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

The code for our website looks like this

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

3. Redirect Only a Specific Folder

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

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

Replace “yourdomain” with your actual domain name(eg: easywpknowledge.com), wherever required. Also, in the case of the folder, replace it with the actual folder name.

ssl activated

Leave a Comment

Your email address will not be published. Required fields are marked *