Redirect Default www with .htaccess

This tutorial explains how to use .htaccess to redirect a website's default URL to the www subdomain. By redirecting users to the www version of your site.

To redirect your site's non-www URL to the www URL using .htaccess, you can add the following code to your .htaccess file:

 # Redirect non-www to www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This code uses mod_rewrite to check if the incoming URL does not start with "www.", and if so, redirects the user to the equivalent URL with "www." added. The [R=301,L] flags at the end of the RewriteRule indicate that this is a permanent redirect and that this is the last rule that should be applied.

Make sure to test your site thoroughly after adding this code to your .htaccess file to ensure that the redirection is working correctly.