How to manipulate default document with rewrite module on IIS7

iis-7rewriteurl

Until few month ago i've been using IIS 6 where i could add different Default Documents to each websites which are physically same directory. Since II7 which adds Default Document value to web config i couldn't use such technique as web.config was changed for all the directory.

So I've found simple solution with rewrite module to change Default Document for each domain

 <defaultDocument enabled="false" />
        <rewrite>
            <rewriteMaps>
                <rewriteMap name="ResolveDefaultDocForHost">
                      <add key="site1.com" value="Default1.aspx" />                    
        <add key="site2.com" value="Default2.aspx" />
                </rewriteMap>
            </rewriteMaps>
            <rules>
    <rule name="DefaultDoc Redirect If No Trailing Slash" stopProcessing="true"> 
      <match url=".*[^/]$" /> 
      <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" /> 
      </conditions> 
      <action type="Redirect" url="{R:0}/" /> 
    </rule> 
    <rule name="PerHostDefaultDocSlash" stopProcessing="true"> 
      <match url="$|.*/$" /> 
      <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" /> 
     <add input="{ResolveDefaultDocForHost:{HTTP_HOST}}" pattern="(.+)" /> 
      </conditions> 
      <action type="Rewrite" url="{R:0}{C:1}" appendQueryString="true" /> 
    </rule> 
            </rules>
        </rewrite>

Now i've got two other issues.

first of all i can't use canonical url rewrite, if i set one then site1.com and site2.com redirected to www.site1.com instead of having www. for each.
second problem is that there is a directory within site1' and site2' physical directory called members in which Default.aspx is always a Default Document doesn't matter which domain name was used. It doesn't work as well.

Please help me with this issue because i've never thought i will get such problem with supposed to be better IIS7…

Best Answer

You have two options. One is to keep going down the path that you're on. At a high level the issue with the canonical url rewrite issue is with the action. You can use {HTTP_HOST}, so the action should be something like www.{HTTP_HOST}/{URL}. That makes it dynamic based on the domain name entered rather than having it set to a specific name.

However, you may find it easier to continue to use the default docs. The way to do this per site is to use location tags within applicationHost.config. You can't do it the standard way within IIS Manager because, as you pointed out, it shares the same config for all sites. There are 3 fairly easy ways to do it (not counting the programming options). One is with Configuration Editor, another is with appcmd and the 3rd is with notepad or an editor directly.

I'll describe here how to do it the Configuration Manager way. First, make sure that you have it installed: http://www.iis.net/download/AdministrationPack

Then using IIS manager, navigate to the site you want to change the default doc for. Open Configuration Manager. In the top left drop-down, change to system.webServer/defaultDocument. In the top right, make sure to change to the ApplicationHost.config option. In the main window, select the 'files' row and click the (...). The rest is straight forward.

Related Topic