Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

IIS Web.config: redirect non-www to www

Hi All,

Could someone help me with the web.config code i need to ensure all websites are prefixed with www.

Many thanks
D
Avatar of Tom Cieslik
Tom Cieslik
Flag of United States of America image

To redirect requests without the www to www on your website, add the following rules to your web.config.
Make sure your.domain.name below is changed to your domain name.
NOTE: If you already have a web.config with rules configured, add the rule below, otherwise a full web.config example is below it.

Rule
=========================================================================

        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^your.domain.name$" />
          </conditions>
          <action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />
        </rule>

Full Web.Config with Rule
==========================================================================

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^your.domain.name$" />
          </conditions>
          <action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Avatar of detox1978

ASKER

thanks.   I have multiple websites.  is there a catch all?
What do you mean multiple websites ?
Are they on same domain ?

Redirection is working from non www domain and forwarding request to www.doman.com
If you have few websites with different name like

www.domain.com
www1.domain.com
www2.domain.com
then you need to create more rules but if user will put http://domain.com in browser then only first rule will work.
you could use the redirection function in IIS. See the link below

https://technet.microsoft.com/en-us/library/cc770409(v=ws.10).aspx
ASKER CERTIFIED SOLUTION
Avatar of Dan McFadden
Dan McFadden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial