Return a 404 Not Found status code when the ItemNotFound page is loaded

20 November 2012 at 00:00 by Ruud van Falier - Post a comment

When Sitecore can't find the requested item, it will redirect the user to the page that is configured in the setting ItemNotFoundUrl.
The problem is that redirecting will return an HTTP status code 302 (Moved Temporarily) and what we really want is the status code 404 (Not Found).

I've seen solutions where people will set the status code on the 'ItemNotFound' page.
That does not solve anything because the client will still be redirected to the ItemNotFound page and the initial request will not be marked as not found.

To solve this problem you can implement this custom pipeline processor.
It overrides the method that is called when Sitecore redirects the client to the ItemNotFound page.
Instead of redirecting, it does an HTTP request to retrieve the content of the ItemNotFound page and then it writes it to the client together with a 404 Not Found status code.

Keep in mind that:

  • The hostname of your website should be available on the server (some servers don't have a DNS, in that case you can add it to the hosts file)
  • Information about cookies is not submitted during the internal request of the ItemNotFound page, so if you have personalised information on that page, it will not work

To implement my solution,
add this to your include config file or add the processor to your web.config as a replacement of the default ExecuteRequest processor:

    <processor type="ParTech.Library.Pipelines.ExecuteRequest, ParTech.Library" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']"/>
    <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel">
      <patch:delete />

Add this class to your solution:

namespace ParTech.Library.Pipelines
    public class ExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
        protected override void RedirectOnItemNotFound(string url)
            var context = System.Web.HttpContext.Current;

                // Request the NotFound page
                string domain = context.Request.Url.GetComponents(UriComponents.Scheme | UriComponents.Host, UriFormat.Unescaped);
                string content = Sitecore.Web.WebUtil.ExecuteWebPage(string.Concat(domain, url));

                // Send the NotFound page content to the client with a 404 status code
                context.Response.TrySkipIisCustomErrors = true;
                context.Response.StatusCode = 404;
            catch (Exception)
                // If our plan fails for any reason, fall back to the base method

            // Must be outside the try/catch, cause Response.End() throws an exception

The result in Firebug after requesting a non-existing item: