This is a complete guide to htaccess redirect http to https.

We will be discussing in detail about-

  1. What is SSL?
  2. Why do you need SSL for your website?
  3. How to edit .htaccess file using FTP or cPanel file manager?
  4. Redirect http to https in Apache
  5. Redirect http to https in NGINX
  6. HTTP to HTTPS redirection in Cloudflare

Let’s Begin

What is SSL?

SSL or Secure Sockets Layer is a standard security protocol that encrypts the connection between the web browser and server. It protects sensitive information like a credit card, login details, and other user information. 

Why do you need SSL for your website?

  • Secure Transactions
  • Boost SEO
  • Protect your site from hackers and phishing
  • PCI Compliance
  • Safeguard Customer’s Data
  • HTTPS browser trust indicator
  • Green Address Bar

How to redirect HTTP to HTTPS by editing .htaccess file?

You need to Install SSL certificate for 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:

  1. Editing .htaccess file via FTP
  2. Editing .htaccess file via cPanel File Manager
  3. Redirect http to https in Apache
  4. Redirect http to https in NGINX

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.

Get Step by Step Guide to Install SSL Certificate here.

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.

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

HTTP redirect to HTTPS in Cloudflare

If you are using Cloudflare, then you can perform HTTP redirection easily.

  • Login to Cloudflare
  • Selection your website
  • Go to Crypto tab
  • Scroll down to “Always Use HTTPS section”
  • Make sure it is turned ON

We hope you find this article useful. But if you are still not able to install and configure SSL on your website correctly, then you can contact us and we will get back to you as soon as possible.

Was this post helpful?