Casbay Knowledge Base

Search our articles or browse by category below

Redirect all traffic to HTTPS using an .htaccess file.

Last updated: September 7, 2022
Estimated reading time: 1 min

Redirect all traffic to HTTPS using an .htaccess file

What is SSL?

Secure Sockets Layer or SSL is a standard security protocol to establish encrypted links and communication between a web server and a browser.

SSL is commonly used to ensure that all data transmitted between the web server and browser remains encrypted.

An SSL certificate is necessary to create 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.

In order to force your web traffic to use HTTPS, you need to edit the codes in the .htaccess file.

Before we move onto redirecting HTTP to HTTPS, click HERE to find out how you can edit .htaccess file. If you already know, you can proceed with this guide.

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.example.com/$1 [R,L]

2. Redirect Only a Specific Domain

To redirect a specific domain to use HTTPS, add the following:

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

3. Redirect Only a Specific Folder

To redirect HTTPS on a specific folder, add the following:

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

Note: Replace “example” with your actual domain name wherever required. Also, in case of the folder, replace /folder with the actual folder name.

We hope this article helped you to learn How to redirect traffic to HTTPS using .htaccess file. For more articles, please go to Knowledge Base.

Was this article helpful?
Dislike 1
Discover the perfect balance of performance and budget-friendly Dedicated Server plan !
Discover the perfect balance of performance and budget-friendly Dedicated Server plan !
High performance and low cost Dedicated Server plan 128GB from $185 – upgrade today!
High performance and cheap Dedicated Server plan 128GB from $185 – upgrade today!