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 Users Found This Useful
Was this answer helpful?

Related Articles

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...

htaccess RewriteRule Examples

Here are some useful mod_rewrite RewriteRule redirect examples that you can use in your .htaccess...

Using htaccess File to Block IPs

If your webserver is Apache, you may block access to your website from specific IP addresses...