Custom Error Pages for files/directories other than .aspx

Hi Guys,

I am about ready to go down to the server room and toss Windows Server 08 IIS 7 machine out the 3rd floor… I am trying to add a custom error page for 404s, but IIS only honors this configuration for .aspx pages. I understand that IIS will handle other requests itself without involving the ASP worker process but how would I include directories and other page types or even PDF files, etc?

I have searched all over and can only seem to find old posts < 2009 which are ambiguous at best. Has anyone successfully configured a custom error page to handle 404s for directories, .html, and file extensions(mainly .pdf)? Another issue is the default operation of the 404s. They re-direct which causes a 302. I simply just want my custom page to display and still respond with a 404.

Thanks in advance and sorry if I missed something obvious. Why does something seemingly simple have to be so diluted with IIS?

The problem is there are parallel configurations in IIS7 – it has lots of backwards compatibility features. The default modern configuration is to run everything through the ASP.NET pipeline but it sounds like you are setup to run in legacy mode where things can work different. It is probably working in this mode for a reason.

Anyhow, if you post the configuration you are using we might be able to help you before you toss said machine out the window.

If it makes you feel any better, that odd semi-xml people call httpd.conf typically makes me want to puke. nginx is OK though.

you might need to write a IhttpHandler for this.

No doubt MS could make this simple thing lot easier then this.


Excuse me for my n00bness with IIS 7, but what is the best way to gather the configuration? I’ll have to get with the “Gremlins” in MIS(Network Admins) to pull the information. The bad part is they do not have much experience with IIS either, so I’ll get to wear two hats, but they still lock it down pretty good.

Are you meaning just the web.config file?

Many thanks in advance.


Do you know of any good sources for docs on making this work with a generic httphandler? I have dabbled a bit with them to handle restricted files due to the fact my predecessor not using the built in security, but not with this scenario.

IIS7 config per site is generally in the web.config file. They should be able to send you that and that might at least show the problem. Basically there are 2 places to designate errors in that file, make sure you are working in System.WebServer not the other options.

One of the really nice things about IIS7 is the local version you’ve got on your vista or win7 desktop is the same as the server sku in most practical ways so you should be able to setup a local copy and test scenarios and tell the “gremlins”. Furthermore, it is very easy to delegate specific IIS admin privileges now.

No, you don’t need a IHttpHandler to do this. At least not on IIS7.

Sorry for the late reply, but the wheels of the corporate engine grind slowly… Here is the config section where I am trying to initiate the custom 404. I do notice it is in the system.web section, not the system.webserver.

This used to go to a page that handles landing pages(i.e. where the 404 filter was redirecting their to check for a landing page and pushed back to the 404 page if it could not find one by taking the last part of the URI.

Even after changing this to just go to the 404 page, it is still re-directing to the landing page. Is this cached somewhere?

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

I also noticed there is a catch all of any unhandled exception in the same parent tag…

<add type="UnhandledExceptionModule" name="UnhandledExceptionModule" />

But I think this is only for uncaught application exceptions, not 404s.

My problem is that it is still returning a 302 because of the re-direct and not a straight 404. So as far as Search engines are concerned they are soft 404s because content is still being served up. I tried uploading a screen shot but received an error on the editor(Error#2038).

Where in IIS can I find if I am in classic pipeline or integrated?

Thanks in advance, I am stumped a little here.

i tried this with a dedicated server where I had to setup the 404 script handler changed to a custom one e.g. mycustomhandler.aspx. this was done on iis manager.

then on mycustomhandler.aspx code behind oninit:
checked if the status was 404. if it was 404 I redirected to the 404.aspx with the proper header.

On IIS6, using an .aspx page for the IIS-panel configured server-side 404 page was a pretty brilliant trick, but that is unnecessary on IIS7.

For IIS7, what you need to do is set error pages in the system.webserver section of your web.config. I’d try:

		&lt;remove statusCode="404" subStatusCode="-1"/&gt;
		&lt;error statusCode="404" prefixLanguageFilePath="" path="/MyErrorPage" responseMode="ExecuteURL"/&gt;

It is hard to tell from there if you are running in integrated mode. That is an app pool level setting, ask the server guys which pipeline you are using.