iis-7windows-server-2008hostheadershostheader

IIS Host Headers and non WWW to WWW


I know there's a bunch of examples on how to redirect your non www to your www site, but I'm not using any rewrite utils/ISAPI.

On my Windows 2008R2 box, I have several sites setup in IIS. I setup host headers for both www and non www versions. The first couple of sites work fine. If you try to go to the non www site, you are automatically redirected to the www version.

As far as I recall, I didn't have to do anything special other than add the appropriate host headers - no messing around with rewrites/ISAPI.

What am I missing on the server manager side of things in order to get this working?


Solution

  • I guess there are two ways. One is to create a rewrite rule through the IIS manager.

    The other is to setup the system.webserver section of the web.config as follows:

      <system.webServer>
    
        <rewrite>
          <rules>
            <clear/>
            <rule name="Redirect Non WWW to WWW" enabled="true" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
              </conditions>
              <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
            </rule>
    
            <!--<rule name="Default Document" stopProcessing="false">
              <match url="(.*)default.aspx"/>
              <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
            </rule>-->
    
          </rules>
        </rewrite>
    
        <validation validateIntegratedModeConfiguration="false"/>
        <modules runAllManagedModulesForAllRequests="true"/>
    
        <httpErrors errorMode="Custom"/>
    
      </system.webServer>