Sitecore 404 easy and SEO friendly

Within Sitecore there are several way to implement the 404 page within a multisite platform.

My favourite way is having the 404 page as a Sitecore item to allow the marketeers to have control of the content and ensure that the page has the same look and feel of the web site….

At the same time the SEO recommendation is to return a 404 status code to notify bots about it and differentiate from a content page.

Sitecore can handle the redirect to a 404 page, but it is not able to set the status code to 404, in order to do it, you need to customize the ExecuteRequest Pipeline…
This is the right code to handle correctly the 404 page and set the http status code

using System;
using System.Web;
using Sitecore.Configuration;
using Sitecore.Web;
using scPip = Sitecore.Pipelines;
       
namespace XXX.Sitecore.Platform.Core.Pipelines
{
       
   public class ExecuteRequest : scPip.HttpRequest.ExecuteRequest
       {
          protected override void RedirectOnItemNotFound( string url)
              {
                var context = System.Web. HttpContext.Current;
                  try
                     {
                            // Request the NotFound page
                            string domain = context.Request.Url.GetComponents(UriComponents .Scheme | UriComponents.Host, UriFormat.Unescaped);
    string content = 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;
                           context.Response.Write(content);
                     }
                      catch ( Exception)
                     {
                     // If our plan fails for any reason, fall back to the base method
                            base.RedirectOnItemNotFound(url);
                     }

            // Must be outside the try/catch, cause Response.End() throws an exception
                     context.Response.End();
              }
               ///
<summary>
               /// Redirects request to the specified URL.
               /// </summary>

               /// <param name="url"> The URL.</param>
               protected override void PerformRedirect( string url)
              {
                      if ( Settings. RequestErrors.UseServerSideRedirect)
                     {
                            HttpContext.Current.Server.TransferRequest(url);

                     }
                      else
                            WebUtil.Redirect(url, false);
              }
       }
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" >
  <sitecore>
<pipelines>
      <httpRequestBegin>
 <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest,Sitecore.Kernel">
<patch:attribute name="type">XXXX.Sitecore.Platform.Core.Pipelines.SundioExecuteRequest, XXX.Sitecore.Platform.Core</patch:attribute>
        </processor>
      </httpRequestBegin>
    </pipelines>
    < settings>
      < setting name ="ItemNotFoundUrl" value="/404"/>
         < setting name ="LinkItemNotFoundUrl" value="/404"/>
         < setting name ="LayoutNotFoundUrl" value="/404"/>
         < setting name ="RequestErrors.UseServerSideRedirect" value="true"/>
       </ settings>
  </sitecore>
</configuration>
Advertisements

testing Commerce Server Pipelines

this is a great resource to learn how to instrument your own project to test PipeLines…
http://csdevwiki.codeplex.com/SourceControl/changeset/view/28029#

if your CS implementation is correct, you will be able to test 95% of your business logic testing your pipelines, testing pipelines, it could be very useful to verify that a new discount is interacting correctly with the existing ones, and that’s a way to automate your discount testing procedure.