How to Force Redirect HTTP to HTTPS by Editing .htaccess File?

You need to Install SSL certificate to force htaccess redirect to HTTPS. The first step involves editing of .htaccess file after successful installation of SSL. You can do it via FTP or cPanel File Manager. The second step involves htaccess redirect to https in Apache or NGINX.
Insert this code to the .htaccess file and force all web traffic to use HTTPS. 
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
Don’t worry if you don’t know how to access the .htaccess file, keep reading.

Steps to Redirect Http to Https with htaccess

Steps to Edit .htaccess File

There are specific instructions in the .htaccess file that informs the server how to behave in particular cases and directly affects your site functionality. Two primary directives in a .htaccess file are Redirects and Rewriting URLs.

Editing .htaccess via FTP

  • Edit the file on your computer and upload it to the server using FTP.
  • Use “Edit” mode in the FTP program that allows you to edit a file remotely.
  • Make use of a text editor and SSH to edit the file.
htaccess redirect to https in apache and Nginx

Editing .htaccess in cPanel File Manager

Note- Backup your website in case something goes wrong.
  • Login to cPanel
  • Files > File Manager > Document Root for:
  • Now select the domain name you want to access
  • Check “Show Hidden Files (dotfiles)”

Edit .htaccess in cPanel htaccess redirect to https

  • Click “Go”
  • After a new tab or window opens, look for the .htaccess file.
  • Right-click on the .htaccess file and click on “Code Edit” on the menu.
  • A dialogue box may pop up asking about encoding. Click the “Edit” button to continue.
  • Edit the file.
  • “Save Changes” when done.
  • Test your website to make sure it is done correctly. In case, there is an error, restore to the previous version and try again.
  • Once you are done, click “Close” to close the window.
HTTP vs HTTPS

How to Redirect http to https In Apache?

How to Redirect http to https In Apache?

1. Redirect All Web Traffic

If you have existing code in your .htaccess, add the following:
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]

3. Redirect Only a Specific Folder

Redirecting to HTTPS in a particular folder, add the following:
Services we offer:RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
Note: Replace “yourdomain” with your actual domain name wherever required. Also, in case of the folder, replace /folder with the real folder name.

How to do http to https redirect in NGINX?

  1. Login to NGINX web server
  2. Add return 301 https://$server_name$request_uri in server directive.
  3. Save the file.
  4. The last step is to restart your NGINX web server.
Note- Take a backup of nginx.conf or default.conf file
Phew! It’s done.
Think it was useful? Share this article to help people come on HTTPS 🙂