Use this code may be it will help you -
Redirect Using .htaccess
If your site is hosted on Apache, you can redirect from the WWW to the non-WWW, or vice versa, with a few lines in your .htaccess file.
Redirect WWW to non-WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(yourdomain.com)?$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
Redirect non-WWW to WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.yourdomain.com)?$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
or
Redirect Using IIS7
With IIS7, there are actually two ways to do this. The URL Rewrite extension is required for this.
The first method involves adding the following as the first rule in the system.webServer section of the web.config file of the site in question.
Redirect WWW to non-WWW:
<rewrite>
<rules>
<rule name="www to non www"" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.yourdomain\.com$" />
</conditions>
<action type="Redirect" url=http://www\.yourdomain\.com/{R:1}” redirectType="Permanent" />
</rule>
Redirect non-WWW to WWW:
<rewrite>
<rules>
<rule name="non www to www" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.youdomain\.com$" />
</conditions>
<action type="Redirect" url="http://www\.yourdomain.\com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>