mod_rewrite

Add www to URL using Apache mod_rewrite

For sites belonging to a domain name(Not Subdomain), one can access the site using the domain name with www, Ex: www.example.com or without it: example.com

Both ways the site can be accessed when a ServerAlias is created in the config. However this is confusing as session information for both is different, which means if one is accessing the site with www preceded and logs in, on accessing the site without www preceded - the server would have no information saved.

To redirect any domain name to use www by default, the following mod_rewrite condition can be used: