Posts Tagged ‘mod rewrite’

how to add www to your domain name using .htaccess file

Thursday, June 19th, 2008

If you are wanting to add www. to your domain name automatically such as changing http://yourdomain.com to http://www.yourdomain.com then you can use a few simple lines in your .htaccess file in your public_html folder. This is especially useful for SEO as search engines index the domain with and without the www. as seperate sites.

Note that you will need the mod rewrite plugin active in your apache configuration in order to do this

#Turn on rewriting engine
RewriteEngine on
# If HTTP_HOST starts with yourdomain.com
# (Note: In a regular expression you have to escape the .
# with the backslash)
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
# change the URL to the URL with www prepended and
# use a permanent redirect
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

################################################
# ALL LINES STARTING WITH A # ARE COMMENTS,    #
# AND SHOULD BE REMOVED IN YOUR VERSION        #
#                                              #
# LEAVING THEM IN BETWEEN THE RewriteCond and  #
# RewriteRule LINE WILL CAUSE THE FILE TO      #
# FUNCTION INCORRECTLY                         #
################################################

.htaccess file generator

Enter your domain name below without the www like yourdomain.com and click submit to generate your htaccess file