ASP.NET Custom Error not throwing 404

So I had an issue with Google the other day when I noticed my site was no longer verified.
So I went through the general process of verifying but Google complained that it was not seeing a 404 with the following message

We've detected that your 404 (file not found) error page returns a status of 200 (Success) in the header

Now I found this very strange, as my custom errors handle my 404 so what's the problem. I had setup my web.config as follows:

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

So back to using Fiddler, I reviewed the status codes being returned when hitting my 404 page.
No suprise, a 302 was being returned instead of a 404. More specifically, in my rewriting environment this was returning a 200 which google was complaining about.

Now this only occurred when running in IIS, and when the missing page ended with an extension handled by the aspnet_isapi.dll.
So with re-writing turned on, that's EVERYPAGE in my entire website. Great!
Now as a side note, In Casini Web Server it seems a 302 is always thrown. So you can only replicate this through IIS.

So, onto THE FIX

Quite a simple one, we just need to specify the status code on our 404 page.
So I use a WebForm as my 404 and I can now set the status code in the code behind:

Response.StatusCode = 404;

This immediately fixed my problem and Google is happy again ... Don't we all love making Google happy

I searched around for a way to do this in an HTML file, but could not find one. If anyone knows please comment and I'll update the post.


posted @ Wednesday, August 27, 2008 12:59 PM