views:

5333

answers:

6

After trying to setup my site for Google Webmaster Tools I found that my Custom ASP.NET 404 page was not returning the 404 status code. It displayed the correct custom page and told the browser that everything is OK. This is consider a soft 404 or false 404. Google doesn't like this. So I found many articles on the issue but the solution I want didn't seem to work.

The solution I want to work is adding the following two lines to the code behind Page_Load method of the custom 404 page.

Response.Status = "404 Not Found";
Response.StatusCode = 404;

This doesn't work. The page still returns 200 OK. I found however that if I hard code the following code into the design code it will work properly.

<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="server">

<%
 Response.Status = "404 Not Found";
 Response.StatusCode = 404;
%>

 ... Much more code ...

</asp:content>

The page is using a master page. And I am configuring custom error pages in my web.config. I would really rather use the code behind option but I can't seem to make it work without putting a the hack inline code in the design / layout.

+11  A: 

Solution:

The problem, it turned out, was the use of the master page. I got it to work by setting the status code later in the pages lifecycle, obviously the rendering of the master page was resetting it, so I overrode the render method and set it after the render was complete.

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
    Response.StatusCode = 404;
}

More work could be done to find out exactly when the master page is setting the status, but I'll leave that to you.


Original Post:

I was able to get a test web app to work fine, well it at least displayed the custom error page and returned a 404 status code. I can't tell you what is wrong with your app, but I can tell you what I did:

1) Edited the web.config for custom errors:

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

2) Added a 404.aspx page and set the status code to 404.

public partial class _04 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
    }
}

Thats about it, if I go to any page extension that is processed by Asp.Net and does not exist, my fiddler log clearly shows a 404, here is the header:

HTTP/1.1 404 Not Found
Server: Microsoft-IIS/5.1
Date: Sun, 07 Dec 2008 06:04:13 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 533

Now if I go to a page that is not processed by Asp.Net, like a htm file, the custom page does not show and the 404 that is configured by IIS is displayed.

Here is a post that goes into some more details that may be of use to you and your problem, my test does do a redirect to the new page so the url of the requested file is pretty much lost (except its in the query string).

http://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages

Header Spy Response:

HTTP/1.1 404 Not Found
Date: Sun, 07 Dec 2008 06:21:20 GMT
Ryan Cook
What does the browser state? I use the addon Header Spy for Firefox.
Bobby Cannon
This still doesn't appear to fix my problem. I will do more checking...
Bobby Cannon
Works on my development server but still will not work on my host...
Bobby Cannon
This works locally so I give you the answer. I really do appreciate it. Thanks!
Bobby Cannon
No problem. I hope you get it going on your host.
Ryan Cook
Thankfully, this is at the top of Google's results for this problem :D
noluckmurphy
A: 

What does the browser state? I use the addon Header Spy for Firefox.

Bobby Cannon
Header Spy Response:HTTP/1.1 404 Not FoundDate: Sun, 07 Dec 2008 06:21:20 GMT
Ryan Cook
Are you using a master page? Maybe that's it. I will try a page without using a master page...
Bobby Cannon
No I wasn't, but I can do a quick check as well, I'll try it with one.
Ryan Cook
Yep, thats it! The master page caused a 200 OK
Ryan Cook
So I need to figure that part out, wonder how to fix?
Bobby Cannon
Got it to work, updating my post now.
Ryan Cook
OK, post updated, I hope it works for you.
Ryan Cook
+3  A: 

After much testing and troubleshooting it appears that certain hosting providers can interfere with the return code. I was able to get around this by applying a "hack" in the content.

<%
// This code is required for host that do special 404 handling...
Response.Status = "404 Not Found";
Response.StatusCode = 404;
%>

This will allow the page to return the correct return code no matter what.

Bobby Cannon
+2  A: 

Hello,

I had a similar problem I want to show a custom page as a 404 (which is ASPX) and it worked fine on localhost but as soon as a remote visitor connected they would get the generic IIS 404.

The solution to this was to add

Response.TrySkipIisCustomErrors = true;

Before changing the Response.StatusCode.

Found via Rick Strahl http://www.west-wind.com/weblog/posts/745738.aspx

gary
A: 

Try calling Response.End() to skip rendering...

Response.Status = "404 Not Found";
Response.StatusCode = 404;
Response.End();
return;
Jason Goemaat
A: 

In PHP you can add these to the top of your custom 404 page to force it to send a 404 response.

header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");

We have a simple tool to check if your custom 404 page is indeed returning HTTP 404 status. http://getinnepal.com/blog/web/return-404-status-custom-404-page/

GetIn Nepal