问题
Using IIS 7.5 I have created a custom 404 (and 403.14) error page that displays content from a database if a static file is not found.
In other words, if I browse to http://mysite.com/test/ and a physical index or default file is not found at that location then IIS executes my 404 custom error page which parses the url and either displays a page stored in a database, or a notice saying the page cannot be found.
Everything is working perfectly except that IIS will not display the 404 page for the web root index/default page.
There are no files in the web root directory - here are my observed results:
- mysite.com: Completely blank page
- mysite.com/: Completely blank page
- mysite.com/index.aspx: Correctly displays my custom error page.
- mysite.com/default.aspx: Correctly displays my custom error page.
- mysite.com/index.htm: Correctly displays my custom error page.
- mysite.com/anything_else/: Correctly displays my custom error page.
My Web.config file contains:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/_page/" responseMode="ExecuteURL" />
<error statusCode="403" subStatusCode="14" path="/_page/" responseMode="ExecuteURL" />
</httpErrors>
I would like to store the contents of the home page in a database like the rest of the site's dynamic pages but after much searching still cannot find out how to get IIS to display my custom error page when the home index/default page is not found.
Any help/thoughts would be greatly appreciated.
fodder
回答1:
I ended up solving this by using the web root index itself as the custom error page instead of another page like "/_page/".
This worked for everything except for SSL redirecting, which I decided was not a big enough issue to hold up the project.
In the system, every page is explicitly set to either http or https. If a user navigates to an https page using an http address then they are redirected to the https version and vice-versa. However, using the root index as the custom error page means that this redirection won't work for the home page because IIS will not redirect a page to itself for some reason.
If anyone has any idea how to get around that problem I'd greatly appreciate your thoughts. But the original question is essentially solved.
Hope reading this helps someone.
fodder
UPDATE - 08/17/2012:
Well, it took a support call to Microsoft (arduous, but eventually fruitful) but I finally found the answer to the underlying problem
When I first set up the web server, I did not install the "Directory Browsing" module for security reasons. I didn't want web clients to be able to see the file structure.
Well, it turns out that one needs to install the Directory Browsing module (EVEN IF IT IS DISABLED) to make IIS respond with anything besides a 200OK (and generate a blank html page).
Once installed (and left disabled, for security reasons), IIS now responds with a 403.14 Forbidden error, which I already had correctly redirecting in my web.config.
来源:https://stackoverflow.com/questions/9997772/iis-7-5-custom-404-error-page-not-working-for-web-root-index-default