HTTP to HTTPS redirection using .htaccess file

Without an SSL you can’t perform HTTP to HTTPS redirection. I have always suggested people to use SSL. Now the question is why HTTPS redirection is so important?
Does HTTPS redirection help in website SEO? What will be the effect on my website if I don’t use HTTPS? So, let’s see the importance of HTTP to HTTPS redirection.
{Read:- Best PWA WordPress Plugins 2021 }
A travel business came to me for SEO on his website. They started complaining that we are not getting any leads from our ads campaign. They were actually running a Google AdWords campaign for getting some calls and signups. But they don’t have HTTPS on their website.
You know in this particular case HTTP to HTTPS redirection is not only the factor that affected the lead. There were a lot of other factors as well. Which I will discuss some other time.
{Read:- How to edit ads.txt without Cpanel in WordPress }
Things to do before doing HTTP to HTTPS redirection
You know, you can’t perform a HTTPS redirection without having a valid SSL. So, you have to install SSL first if you want to do HTTPS redirection.
If you don’t know how to install an SSL certificate. You can refer the below article. It will provide a good knowledge base for every server. You can choose accordingly like in my case it is cPanel.
HTTP to HTTPS redirection using .htaccess file
In most of the cases, when you have domain, hosting and SSL from the same provider. You need need to perform HTTPS redirection. But, in cases it is required to do it manually.
{Read:- 5 Best tips to Grow Your SEO Audience 2021 }
So, this can be done easily by the .htaccess file. You can find this file in your root directory.
Ways to edit an .htaccess file:
- Use a text editor and SSH to edit the file.
- Use the File Manager in cPanel to edit the file.
- Edit the file on your computer using notepad or notepad++ and upload it to the server using FTP.
- Use the “Edit” mode in the FTP program that allows you to edit a file remotely.
In my recommendation, the better way to do it is to “use the file manager in cPanel.”
Editing .htaccess in cPanel File Manager
- Login to cPanel, use domainname.com/cpanel
- Open File Manager
- Check “Show Hidden Files (dotfiles)” in case if you don’t find the .htaccess file. In the case of multi-hosting, you have to go to the relevant website folder
- Click “Go”
- After a new tab or window opens, look for the .htaccess file
- Right click on the .htaccess file and click on “Edit” on the menu (refer the below image)
- A dialogue box may pop up asking about encoding. Click “Edit” button to continue
- Edit the file accordingly the codes suggested below
- Click on “Save Changes” when done
- Test your website to make sure it is done correctly. In case, there is an error, restore to the previous version and try again
- You can now close or logout if you don’t need
Redirecting HTTP to HTTPS using .htaccess
1. Redirecting All Web Traffic
If you want to redirect all your website traffic to HTTPS then add the following code into your .htaccess file. You can enter these codes between # BEGIN WordPress and # END WordPress. Like this –
Redirection for HTTPS and WWW both
# BEGIN WordPress
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
# END WordPress
{Read:- Googlebot SEO Benefits 2021 }
Redirection for only HTTPS
# BEGIN WordPress
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
# END WordPress
2. Redirecting a specific domain only
For redirecting a specific domain to use HTTPS, add the following code.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
3. Redirecting a specific folder only
For redirecting to HTTPS on a specific folder, add the following code. By the way this is not required most of the time.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
{Read:- What to do for Fast Google Indexing 2021 }