Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Jul 25 '10 at 4:18

This question came from our site for professional and enthusiast programmers.

1 Answer 1

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>
share|improve this answer
    
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 '10 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. –  Franci Penov Jul 22 '10 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 '10 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. –  Franci Penov Jul 22 '10 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 '10 at 3:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.