How to Redirect a Domain Using an Apache .htaccess File

Why Redirect From Your www Domain to Your www-less Domain

For example, why would you redirect from www.smartwebdeveloper.com to smartwebdeveloper.com.

To search engines these are two different domains with identical content.

Your incoming links may get spread between the two domains – some links to pages in the www-prefixed domain and some links to the www-less domain.

Your SEO link juice gets spread between two sites. You could end up with two sites with two different page ranks, instead of a single site with a higher page rank than both.

Forward www to www-less Domain Using .htaccess

A good solution is to forward all pages at the www domain to the non-www domain. An HTTP response code of 301Moved Permanently – is the best response, because it says “this page isn’t here, this is it’s permanent URL”, because Google will still count any links to the www-prefixed domain towards your sites rankings.

To do this, in the root HTML directory for the www-prefixed domain, create a .htaccess file like the following:

#
# [taz] Redirect requests for www.smartwebdeveloper.com to
# smartwebdeveloper.com using Apache's rewrite engine.
#
RewriteEngine on
# [taz] Redirect requests for www.smartwebdeveloper.com,
# with an optional terminating '.' (DNS domains can be terminated with '.'),
# and optionally ending with :port (e.g. ':443' for SSL).
RewriteCond %{HTTP_HOST} ^www\.smartwebdeveloper\.com\.?(:[0-9]*)?$ [NC]
# [taz] Do a HTTP 301 redirect (i.e. page permanently moved)
# to the same URI at smartwebdeveloper.com.
RewriteRule ^(.*)$ http://smartwebdeveloper.com/$1 [R=301,L]

If you have troubles getting the .htaccess to work, try this .htaccess troubleshooting article.

Tested on CentOS 5.3.

Tagged , . Bookmark the permalink.

One Response to How to Redirect a Domain Using an Apache .htaccess File

  1. Sudhir says:

    It Works……Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *