Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Custom error page for 404 returns 302 for http status

Tags:

.net

asp.net

In my asp.net web site I have custom error pages defined as following in my web.config file.

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

When file is not found it correctly display 404.htm page but the issue is when I do Fiddler trace it returns 302 as HTTP status code.This is a big issue for search engine page indexing due to this lot of broken links still have been indexed recently because of this in my web site. how can I prevent returning 302 as HTTP status code for file not found errors and return 404 for file not found errors.I am using asp.net 3.5.

like image 311
DSharper Avatar asked Nov 29 '10 09:11

DSharper


2 Answers

After Googling about this issue, it seems that this is the default behavior that Microsoft ASP.NET provides for the situation. This is very bad for SEO. A work around I found is to check whether the requested file exists in an HTTP handler (or global.asax file), or use:

<customErrors mode="On" redirectMode="ResponseRewrite">
    <error statusCode="404" redirect="/FileNotFound.aspx" />
</customErrors>

If the requested file does not exist, then rewrite the request path to a file not found page (if using an HTTP handler or global.asax), clear the server errors on the 404 error page code behind, and add a 404 error header to the response manually rather than waiting for server to do so.

Server.ClearError();
Response.Status = "404 Not Found";
Response.StatusCode = 404;
like image 50
DSharper Avatar answered Sep 22 '22 02:09

DSharper


In ASP.NET 4.0 you can use redirectMode="ResponseRewrite" to send nice error pages and the proper HTTP code.

like image 29
Luke Puplett Avatar answered Sep 22 '22 02:09

Luke Puplett