-1

I have created a website www.abc.com i want if user just enters abc.com than (without www.) he can access my site and i have achieved this part. Now I want that when user just types abc.com the url automatically changes to www.abc.com please help me on this

Thanks in Advance

3 Answers3

3

In the <system.webServer> section of your web.config, add the following:

Wildcards

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="domain.com" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

For more info

coder
  • 13,002
  • 31
  • 112
  • 214
1

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>
1

Another option is to go to your domain server and add a re-direct there. Leave your Web Server untouched.

Hiệp Lê
  • 31
  • 2