How to force HTTPS or WWW using the htaccess file?

Today, We want to share with you htaccess force www.In this post we will show you Redirect to https and www, hear for Redirect to https and non-www we will give you demo and example for implement.In this post, we will learn about SSL forcefully Redirect HTTP to HTTPS Using .htaccess File with an example.

How to Force www or non-www in htaccess?

If you want to simple easy step to redirect non-www requests to the www URL of your website (i.e. redirect from http://your-domain-name.com to

http://www.your-domain-name.com/) then you can do this with a some added lines in your .htaccess file

Example 1:
Insert the following lines under the RewriteEngine On:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Note: Please, Don’t forget to enable the rewrite engine on your .htaccess file !

.htaccess – how to force “www.” in a Best way?

Example 2:

RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force appending www. to only http secure (https://) requests

	Options +FollowSymLinks
	# turn mod_rewrite on
	RewriteEngine On
	
	# If https send to https://www.
	RewriteCond %{HTTP_HOST} ^your-domain-name.COM$ [NC]
	RewriteCond %{HTTPS} on
	RewriteRule ^(.*)$ https://www.your-domain-name.COM/$1 [R=301,L]

Force appending www. to both http (http://) and http secure (https://) requests

# Redirect non-www to www

	Options +FollowSymLinks
	# turn mod_rewrite on
	RewriteEngine On
	
	# If https send to https://www.
	RewriteCond %{HTTP_HOST} ^your-domain-name.COM$ [NC]
	RewriteCond %{HTTPS} on
	RewriteRule ^(.*)$ https://www.your-domain-name.COM/$1 [R=301,L]

	# If http send to http://www.
	RewriteCond %{HTTP_HOST} ^your-domain-name.COM$ [NC]
	RewriteCond %{HTTPS} off
	RewriteRule ^(.*)$ http://www.your-domain-name.COM/$1 [R=301,L]

I hope you get an idea about .htaccess redirect to https and www.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment