How to Force https Using the .htaccess File

How to Force https Using the .htaccess File

Your website’s visitors should be able to access your site using an SSL-encrypted connection for added security, accessibility, or PCI compliance (Payment Card Industry).

The following are easy steps to force your website to redirect from http to https: The first step is to contact your hosting company to ensure that https is available.

Edit .htaccess File

Ways to edit an .htaccess file:

  • Edit the file on your computer and upload it to the server using FTP.
  • Use “Edit” mode in FTP program that allows you to edit a file remotely.
  • Use a text editor and SSH (Secure Shell) to edit the file.
  • Use the File Manager in cPanel to edit the file. (The one used in this article).

 

Editing .htaccess in cPanel File Manager

  1. Login to cPanel.
  2. Files > File Manager > Document Root of your website.

  1. Now select the domain name you want to access (For primary cPanel domains, the folder is usually ‘public_html’.)

  1. Click ‘Settings in the top-right corner and tick ‘Show hidden files (dot files).

If the file did not appear, feel free to create it by clicking ‘+File’. Make sure to name the file ‘.htaccess’, (starting with the dot).

  1. To open the file, right-click it, then click ‘Edit’. In the next pop-up window, click ‘Edit’as well.

  1. Edit the file.

7. “Save Changes” when done.

 

 

Redirecting HTTP to HTTPS

Redirect all web traffic to https: Edit some set of codes in your .htaccess root file to the ones provided below:

To force all web traffic to use HTTPS, insert the following lines of code in the .htaccess file in your website’s root folder. (Note: If you have existing code in your .htaccess, add this above where there are already rules with a similar starting prefix.

 

RewriteEngine On

RewriteCond %{HTTPS} !on

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$

RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

RewriteRule  ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 

Redirect only specified domain:

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website’s root folder:

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$

RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

If the code above doesn’t work, try removing the first two lines. E.g

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

 

Redirect specified Folder:

If you want to force SSL on a specific folder, insert the code below into an .htaccess file placed in that specific folder:

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$

RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteCond %{REQUEST_URI} folder

RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R=301,L]

Make sure you change the folder reference to the actual folder name. Then be sure to replace www.example.com/folder with your actual domain name and folder you want to force the SSL on.

For further assistance feel free to contact CloudHost Support

Leave a Reply

Your email address will not be published.

(Required)

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com