Breaking

Saturday, September 17, 2022

How to Force HTTPS Using .htaccess (Updated Sep 2022)


How to Force HTTPS Using .htaccess (Updated 2022)



After installing an SSL certificate, your website is available over HTTP and HTTPS. However, it’s better to use only the latter because it encrypts and secures your website’s data. While Hostinger allows users to configure this setting in just one click, you can also use the .htaccess file to force HTTPS connection. This tutorial will show you how.

Forcing HTTPS on All Traffic



One of the many functions you can perform via .htaccess is the 301 redirects, which permanently redirects an old URL to a new one. You can activate the feature to force HTTPS on all incoming traffic by following these steps:

  1.     Go to File Manager in your hosting panel and open .htaccess inside the public_html folder. If you can’t locate it, make sure to create or unhide it.
  2.     Scroll down to find RewriteEngine On and insert the following lines of code below it: 
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  3. Save the changes.


Force www or non-www in your .htaccess file in cPanel

  1. First, log into your cPanel.
  2. Once logged into cPanel, Find the Files category and click on the File Manager icon.
  3. Next, click Settings at the top right corner.
  4. In the Settings Menu, choose the document root (or folder) for the domain you are working with and be sure the checkbox next to Show Hidden Files is checked. Click the Save button.
  5. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option.
  6. You are now in the text editor. Place either selection of code in the file and click on the Save Changes button at the top right corner of the screen. Be sure to replace ‘example.com‘ with your actual domain name.

    NOTE: do not place both selections of code in the file as it will cause an error. Below are examples of code that you can use to implement the desired change to the website.


Force www
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC] 

Force non-www
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]






No comments:

Post a Comment

Best SEO Services in Pakistan

We provide each sort of On Page and off web page Services search engine optimization offerings and enhance up via natural search engine marketing offerings in Sialkot, Wazirabad, Daska, Narowal, Gujranwala, Lahore Pakistan.. When you boost your internet site it ought to be designed and developed in accordance to website positioning factor of view like special content, special pictures and search engine optimisation optimized code. After the growing of your website online right away begin web optimization of your web page the usage of our great search engine marketing provider Company in Pakistan for higher ranking of your site. Many corporations begin their on line companies however they do now not care about special content, key phrases density and photographs quality. They simply solely desire rating of their website on first web page of Google and make investments a lot of moneys on that due to which they frustrated.

Pages