Redirecting HTTP to HTTPS Using .htaccess File Print

  • How to Install/Force HTTPS onto a site
  • 0

Chrome and Firefox have started showing insecure warnings on sites without SSL certificates. Without SSL, your website will show insecure to the visitors. Therefore, using an SSL-encrypted connection for safety, accessibility or PCI compliance reasons is necessary. It becomes very important to redirect from HTTP to HTTPS. 

Learn More: Why SSL is Critical?

In order to force your web traffic to use HTTPS, edit the codes in the .htaccess file. Before we move onto redirecting HTTP to HTTPS, here’s how you can edit .htaccess fileIf you already know skip to Redirection steps.

Redirecting HTTP to HTTPS

1. Redirect All Web Traffic
If you have existing code in your .htaccess, add the following:


1 RewriteEngine On
2 RewriteCond %{SERVER_PORT} 80 
3 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:


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

3. Redirect Only a Specific Folder
Redirecting to HTTPS on a specific folder, add the following:


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

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

Think it was useful? Share this article to help them come on TFhost!


Was this answer helpful?

« Back