Throughout this guide, you'll learn how to quickly redirect your website from HTTP to HTTPS.
As a prerequisite, it is warmly recommended to have installed either a Premium SSL Certificate or a Free LetsEncrypt SSL.
Once, you have confirmed that your domain is working properly over the https protocol without any browser warning you may proceed to follow this guide:
How to redirect http to https from cPanel
1) Login to your cPanel either from your client-area or from the server.
2) Click on the "Domains" icon of your cPanel dashboard
3) Click on "Force HTTPS Redirect" for the desired domain of your hosting account. You may refer to the screenshot below
Upon activation of the button, your website will automatically redirect from the HTTP protocol over to the HTTPS one.
How to redirect http to https from the htaccess file
Alternatively you may redirect your website to HTTPS using the .htaccess file located inside your public_html directory. This method would provide a greater control over the redirection behaviour. We will introduce all the different cases you may encounter while trying to redirect
Edit the htaccess file
First and foremost, all the methods quoted below would require you to know how to open and edit your .htaccess file.
The .htaccess file can be edited or created within any folder of your web server. The configuration directives of multiple htaccess file are executed following a nested logic of priority. A sub folder including a htaccess would prevails over the upper levels htaccess.
To edit the htaccess you may proceed as follows:
- Login to your web hosting account using your favorite FTP client or your cPanel File Manager.
- Locate your .htaccess file and open it using any text editor.
- Append any of the redirection snippet quoted below in this article.
This method would redirect both www and non-www to www https for a given domain.
Redirect http to https non-www
On the opposite of the previous method, the snippet below will redirect from non-www and www to the non-www https address for your domain.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
WildCard Redirect from http to https for one domain
This method would respectfully redirect non-www http to non-www https and also www http to https://www. At a practical level, unless intended and as is, it may cause a duplicate content issue for search engines. A prior method is recommended should such intent should be respected.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect all domains to a specific domain
This will redirect both http with and without www. to one domain of your choosing. This code is to be placed at the root of your web hosting directly (prior to the public_html folder). It will work for all add-on domains.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
Redirect only one domain to https www
On the opposite of the previous method, if you are looking to redirect exclusive one domain from a htaccess file instructing all your domains. You may check the snippet below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
Redirect only one domain to https non-www
Just per the last method, you may redirect one exclusive domain to its non-www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
Redirect your domain to non-www https for a specific directory
Here is a specific case which covers a redirection to https only from a specific directory of your web hosting server.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://domain.com/folder/$1 [L,R=301]
Redirect your domain to www https for a specific directory
This is the same as the previous case but for the www. version.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.domain.com/folder/$1 [L,R=301]
Redirect your domain to https excluding a page
On this example you'll be able to set a redirection from http to https while exluding certain page or even a folder
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/page/?$ [NC]
RewriteRule ^(.+)$ https://www.domain.com/$1 [L,R=301
Redirect your domain to https excluding a specific file string
On this example you'll be able to set a redirection from http to https while exluding some web elements from your website.
While it isn't recommended without some specific intent as it would cause a mixed content warning. Here is how to proceed
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SCRIPT_FILENAME} !image.png [NC]
RewriteRule ^(.+)$ https://www.domain.com/$1 [L,R=301]