Question

I've got a page that checks if a user is logged in or the file is public then pushes a PDF to the browser via Response.WriteFile.

Works great except when Google indexes a file and then we remove the file. So I'm looking at adding a 410 Gone to the Response.Status and then redirecting to our error.aspx page.

Firebug tells me that it gets a "302 Found" status code on the document page when a file is deleted. I'm expecting a 410.

Redirect code is:

Response.Status = "410 Gone";
Response.AddHeader("Location", Request.Url.ToString());
Response.Redirect("error.aspx");

Could someone please tell me what I'm getting wrong please?

Was it helpful?

Solution

Redirection is done by sending a status that indicates that the resource is available somewhere else, such as 301 Moved Permanently or 302 Found. You can't send two status codes in the same response. Either the requested resource does not exist (so you send 410) or it does exist at some other location (so you send 301 or 302 or whatever).

I don't think you should be redirecting to an error page, though, because an error message isn't a separate resource that should have its own URL. If a client requests a file and the file is gone, send a 410 Gone status with the error message as the response body — that way the error message comes back from the URL of the nonexistent file itself. A search engine will see the status code and understand that the file is gone, and a browser will show the response body to the user so he can read the error message.

OTHER TIPS

If you look at the spec for 410 Gone, it states that "no forwarding address is known", so a redirect does not seem valid. You can return that same body on that response that you would from error.aspx if you want human users to see something.

The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent. Clients with link editing capabilities SHOULD delete references to the Request-URI after user approval. If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. This response is cacheable unless indicated otherwise.

The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer working at the server's site. It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time -- that is left to the discretion of the server owner.

You can also use this way, first change your desire status.

Response.Status = "410 Gone"; Response.AddHeader("Location", Request.Url.ToString()); ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectScript", "window.location.href=error.aspx", true);

So, In that you get your desire page and status as well.

This is how I had to do a 301 moved permantently response. It should be similar

    //in Global.asax.cs
    protected virtual void Application_BeginRequest (Object sender, EventArgs e)
    {
        if(Request.Url.Host=="www.earlz.biz.tm" || Request.Url.Host=="earlz.biz.tm" || Request.Url.Host=="www.lastyearswishes.com"){
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location","http://lastyearswishes.com"+Request.Url.PathAndQuery);
            CompleteRequest(); //I believe this is the missing piece in your code.
        }

    }

I have been solving similar issue. If the page with resource is removed from the web, I woul like to tell Google Bot 410 Gone, remove from cache, but I would like to offer an alternative similar page to the visitor.

I have solved it like this:

public ActionResult RealEstate(int id, string title)
{
...prepare the model
if (realEstateModel.Result.OfferState == OfferState.Deleted)
{
    var alternativeSearchResult = PrepareAlternative(realEstateModel);
    return Gone410(alternativeSearchResult, context);                                        
}
else
   return View(realEstateModel);

}

Gone410.cshtml look slike this:

@model Bla.ModelGone410

@{
    Layout = null;

    Html.RenderAction("Index", "Search",
        new
        {
            type = Model.type,
            Category = Model.Category,
            city_id = Model.city_id,
            ...
        });
}

and RealEstate.cshtml:

@model Bla.realEstateModel

@{
    Layout = null;
}

This is realestate view...

This gives the response 410 to google bot and search alternative for the user with no redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top