Search code examples
iisweb-config

IIS 7 set default page for the whole site


I have followed the instruction reported here : https://www.c-sharpcorner.com/UploadFile/francissvk/set-default-page-for-a-website-in-iis421/

What i would like to achieve, is that when user try to visit my site : "https://mysitename.com" it should be redirected to the home page ( "https://mysitename.com/pages/home.aspx"). I don't want to create a root Default.aspx page only to do the redirect, i would like to achieve this behaviour through Web.config.

As said, i tried the instruction in the above link, buy also tried the many solution proposed on this site that more or less suggest to add this configuration to Web.config :

<system.webServer>
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="/Pages/Home.aspx"/>
            </files>
        </defaultDocument>
        <handlers>

I have tried different variation of the path, i have tried :

<add value="/Pages/Home.aspx"/>
<add value="~/Pages/Home.aspx"/>
<add value="./Pages/Home.aspx"/>
<add value="Home.aspx"/>

But allways i get this message error :

403 - Access denied.

If i manually enter in the browser the full page url "https://mysitename.com/pages/home.aspx" then i get no issue (just to point out that the page exists and is working)

I don't understand what i am missing in the configuration


Solution

  • A default page or DefaultDocument in IIS can't do what you want.

    It is a feature that defines which document is loaded when a user requested a URL pointing to a directory on the server without specifying an actual page.

    The value field should be the name of a file in that directory such as index.html or home.asp, it can not point to files in other directories.

    In your case you may be able to use the builtin HTTP Redirect feature, in the GUI enable it and point to 'pages', also check the Only redirect requests to... checkbox

    In your root web.config this may look like this:

     <system.webServer>        
        <httpRedirect enabled="true" destination="pages" childOnly="true" />
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="Home.aspx"/>
            </files>
        </defaultDocument>        
     </system.webServer>
    

    Another option is to use the IIS Rewrite Module which allows you to create more complex rules on how to redirect and rewrite requests. It should be faster because it does work without a HTTP redirect which does back to the browser, but you first need it install it and understand how to use it.