Force to WWW and SSL using .htaccess

Posted By TimesCoding Staff | June 15, 2022 | Blog

Here are the two solutions that force WWW and HTTPS using .htaccess.

Solution 01:

In this case, you do not need to indicate the domain in the rewrite rule. Domain and all subdomains will work perfectly in this rule.

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Solution 02:

In this case, you can indicate the domain in the rewrite rule.

RewriteEngine on
#Force WWW & SSL
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$$1 [L,R=301] 

Author: TimesCoding Staff

TimesCoding Staff is a member of the team, and an expert in HTML, CSS, JavaScript, PHP, Ajax, and WordPress theme development.

Read Next