Forcing HTTP Requests to HTTPS with htaccess


This article provides a guide for redirecting HTTP traffic to HTTPS using your website's .htaccess file.

Redirect All Traffic to HTTPS

To redirect ALL http requests to https, add the following lines to the .htaccess file located in the document root directory (for instance,  /home/account_name/public_html/.htaccess):

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

Force Specific Domain Traffic to HTTPS

To redirect http URL requests to https for a SPECIFIC DOMAIN, add the following lines to the .htaccess file located in the document root directory (typically located at /home/account_name/public_html/.htaccess):

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

Force Specific Folder Traffic to HTTPS

To redirect http URL requests to https for a SPECIFIC FOLDER, add the following lines to the .htaccess file located in the appropriate directory (typically located at /home/account_name/public_html/specific_folder/.htaccess):

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

Tip: Make sure to replace the example data, www.example.com/folder with the appropriate domain and directory.

Related:

  • apache, apache ssl, force ssl, force https, https, ssl, htaccess
  • 5 Пользователи нашли это полезным
Помог ли вам данный ответ?

Related Articles

View Server PHP Environment with phpinfo.php

The phpinfo() function outputs a huge amount of information about the system you're using, such...

How to Create Sudo User on CentOS

This article provides step-by-step setup guide for adding Sudo user to CentOS system. The sudo...

How to Use Sudo

This article provides a guide to using a Sudo user on CentOS server. From the command line,...

Set Server Time Zones with Timedatectl

This article provides a guide to setting the server time and server time zone settings using...

508 Resource Limit is Reached

If your website is on a cPanel hosting account, you may have noticed the following error message:...