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/" />
</customError>

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

Print

Comments on this entry:

# re: ASP.NET Custom Error not throwing 404

Left by KD at 10/7/2008 4:28 PM
Gravatar
I am facing the same problem, but in this case its on Wordpress 2.6.2. My site is: http://hostingwebsitecoupons.com I am totally lost & have tried almost everything. Dont know what to do.. Is there a php equivalent to your fix?? :-(

# re: ASP.NET Custom Error not throwing 404

Left by WEBDUDE at 10/7/2008 5:16 PM
Gravatar
I haven't dabbled much in php, but i'm sure there must be some php server side script to set the status code of a page on your 404 page.

If you find the solution, please post it here so anyone else with you problem will find it to.

Good Luck!!

# re: ASP.NET Custom Error not throwing 404

Left by KD at 10/8/2008 10:06 AM
Gravatar
Thanks for your reply, I will definitely keep you in loop...

# re: ASP.NET Custom Error not throwing 404

Left by Vitaliy at 1/21/2009 6:49 PM
Gravatar
Are you sure you are getting 404 , when you are requesting an aspx page which is not found ?

I have the same set up , but still getting back 302 even though within the 404 page have the following code..


Page.Response.StatusCode = 404

# re: ASP.NET Custom Error not throwing 404

Left by blatant at 1/21/2009 8:58 PM
Gravatar
Yeah, i'm pretty sure.
Off the bat, .net custom 404's will throw a 404 after the 302.
But once you start experimenting with rewriting modules, this can be easily lost.
In my scenario it was resulting in a 200.

The 302 is still wrong however and i've since then written a custom module to throw a 404 first time round by utilising a Server Transfer.
I will be posting on that some day :)

Your comment:



 (will not be displayed)


 
 
 
Please add 4 and 7 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910