How to Redirect Your Domain to HTTPS and WWW Using .htaccess

If you want to ensure that your website always loads with https://www (forcing both HTTPS and www), you can achieve this by adding the following rules to your .htaccess file.

📌 Steps to Implement
Locate your .htaccess file in the root directory of your website.
Open it using a text editor or cPanel’s File Manager.

Add the following code to enforce HTTPS and www redirection:

🔹 .htaccess Code

RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

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

🔍 Explanation
Forcing HTTPS:

If the request is not secure (HTTPS off), it redirects users to the HTTPS version.
Forcing www:

If someone tries to access your site without www (e.g., https://example.com), it redirects them to https://www.example.com.

⚠️ Important Notes
Replace example.com with your actual domain name.
This method ensures all visitors land on https://www.example.com consistently.
The [R=301,L] flag ensures a 301 permanent redirect, which is SEO-friendly.

🚀 Final Thoughts
By applying this .htaccess rule, you improve security (forcing HTTPS) and maintain a consistent domain structure (using www). This is especially useful for SEO and user experience.

Let me know in the comments if you have any questions! 😊