0

Hi I know this question has been answered for other methods but in iis 7 how would one redirect all sites to www version. i.e example.com to www.example.com is there a method or would it be done in the same way as iis 6 where you created another site for example.com and then redirected to www version, or is it best to use code to do this in global.asax or module. Thanks

1 Answer 1

0

Use the IIS URL Rewrite module. In particular, the exact rule you want is:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Canonical host name">
                <match url="^(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://www.example.com/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
8
  • thanks for that, but heres the situation. I have created a binding of www.example.co.uk and anything coming to www.example.co.uk brings up the site, anything coming to example.co.uk brings up default iis page. I have installed the rewrite module and can see it under the site where i have applied the rule and set httpredirect enabled="true" and even exactdestination="true" it does not seem to kick in for some reason
    – khan
    Jul 22, 2010 at 20:01
  • I've noticed that the rule changes for the URL Rewrite module actually take some time to kick in. I believe this is due to the outgoing cache, though I haven't been able to the exact reason. Jul 22, 2010 at 20:53
  • is it correct to have the main binding set to the www version instead of the non-www version
    – khan
    Jul 22, 2010 at 21:40
  • I would either not specify a host name in the binding, or if that's not an option, create a binding for both the www and the non-www versions of the domain. Jul 22, 2010 at 22:16
  • hi again appreciated but i just cannot get it working heres my setup pretty much. Bindings include both the www and none www bindings so the site appears on both, when the following rule is applied the site does not redirect rather it just times out.
    – khan
    Jul 23, 2010 at 3:30

You must log in to answer this question.