TimesCoding

Learn, Practice, and Excel with our Coding Tutorials!

Force to WWW and SSL using .htaccess

June 15, 2022 1 Min read 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 ^(.*)$ https://www.timescoding.com/$1 [L,R=301] 
Related Post: