Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an ASP.NET web-site and to catch wrong urls requests and redirect them to homepage I put:

<customErrors defaultRedirect="/" mode="On">
        <error statusCode="404" redirect="~/"/>
    </customErrors>

in my web.config

This only works for pages like http://mywebsite/wrong.aspx but not for folder (eg: http://mywebsite/wrong-folder/)

I understood that I have to catch all requests in order to solve this but I don't have access to IIS to do that setting with ISAPI...

Can this be done in web.config? Do you have an example?

share|improve this question

The <customErrors>...</customErrors> tag only controls what ASP.NET will do when an error occurs. If you access an non-existing directory the error will be handled by IIS and not by ASP.NET.

You therefore have to change the 404 handling of IIS. This can only be done via the applicationHost.config file though which is normally only accessible with admin rights. The following is an excerpt from that file which changes the 404 handling to a custom page for a specific website:

<location path="[Your Site Name]">
    <system.webServer>
        <httpErrors errorMode="DetailedLocalOnly">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/your-404-handler.aspx" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</location>

You can also change this via the IIS Mananger, via: Sites -> [Your Site Name] -> Error Pages -> 404

share|improve this answer

This can be done by using Microsoft URL Rewrite. If the module is installed, add the following rule to your web.config. Any request not matching "MyPage.aspx" (case insensitive) will respond by issuing an HTTP 301 with MyPage.aspx as the new location. You will need to whitelist all of the acceptable URLs under "conditions" though this is an alternative to using custom errors.

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RedirectToMyPage" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^MyPage.aspx$" negate="true" />
          </conditions>
          <action type="Redirect" url="MyPage.aspx" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Another option is to leverage URL Rewrite and customErrors by configuring customErrors to redirect 404 errors to a page such as "/404Error.aspx" (which doesn't need to exist) and then have a URL rewrite rule that will redirect all requests for "/404Error.aspx" to your homepage. This URL Rewrite rule would look like this:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RedirectToMyPage" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^404Error.aspx$" />
          </conditions>
          <action type="Redirect" url="MyPage.aspx" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.