Tag: 404
Sitecore: Returning a 404 response on the page requested rather than redirecting to a 404 page

Sitecore: Returning a 404 response on the page requested rather than redirecting to a 404 page

Previously I've blogged about:

but while looking through the posts today, I realised I had never written about how you stop Sitecore from issuing 302 redirects to your 404 page and instead return a 404 on the URL requested with the contents of the 404 page.

While search engines will recognise a 302 response to a 404 as a 404 (in fact they're intelligent enough to work out that a 404 page without a correct response status code is a 404) it's considered SEO best practice for the URL to stay the same and to issue the correct status code.

Creating a NotFoundResolver class

When Sitecore processes a request it will run the httpRequestBegin pipeline, and within that pipeline is a Item Resolver processor that will attempt to find the requested item. If after this the context item is still null then the logic to redirect to the ItemNotFoundUrl will kick in. To stop this happening we can simply add our own process to the pipeline after ItemResolver and set the item.

Our class looks like this:

1using Sitecore;
2using Sitecore.Configuration;
3using Sitecore.Data;
4using Sitecore.Diagnostics;
5using Sitecore.Pipelines.HttpRequest;
6
7namespace Pipelines.HttpRequest
8{
9 public class NotFoundResolver : HttpRequestProcessor
10 {
11 private static readonly string PageNotFoundID = Settings.GetSetting("PageNotFound");
12
13 public override void Process(HttpRequestArgs args)
14 {
15 Assert.ArgumentNotNull(args, nameof(args));
16
17 if ((Context.Item != null) || (Context.Database == null))
18 return;
19
20 if (args.Url.FilePath.StartsWith("/~/"))
21 return;
22
23 var notFoundPage = Context.Database.GetItem(new ID(PageNotFoundID));
24 if (notFoundPage == null)
25 return;
26
27 args.ProcessorItem = notFoundPage;
28 Context.Item = notFoundPage;
29 }
30 }
31}

To add our process to the pipeline we can use a patch file like this:

1<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
2 <sitecore>
3 <pipelines>
4 <httpRequestBegin>
5 <processor
6 patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"
7 type="LabSitecore.Core.Pipelines.NotFoundResolver, LabSitecore.Core" />
8 </httpRequestBegin>
9 </pipelines>
10 <settings>
11 <!-- Page Not Found Item Id -->
12 <setting name="PageNotFound" value="ID OF YOUR 404 PAGE" />
13 </settings>
14 </sitecore>
15</configuration>

Notice the setting for the ID of your 404 page to be loaded as the content.

Remember if you do do this make sure you also follow one of the methods to return a 404 status code, otherwise you will have just made every URL a valid 200 response on your site.

Creating a custom 404 Page in Sitecore

Nobody wants to see the standard Sitecore 404 page. Thankfully it's really easy to change the error pages a user is redirected to through some config settings.

Your error pages can be pages in Sitecore or static HTML files. For 404 pages I would normally use a Sitecore page, that way content authors can still manage its content. For an Error Page I would recommend a static html file to avoid issues with the Error page potentially error-ing.

Add these to a patch file and update the urls accordingly:

1<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
2 <sitecore>
3 <settings>
4 <!-- ITEM NOT FOUND HANDLER
5 Url of page handling 'Item not found' errors
6 -->
7 <setting name="ItemNotFoundUrl">
8 <patch:attribute name="value">/ErrorPages/404.html</patch:attribute>
9 </setting>
10 <!-- LINK ITEM NOT FOUND HANDLER
11 Url of page handling 'Link item not found' errors
12 -->
13 <setting name="LinkItemNotFoundUrl">
14 <patch:attribute name="value">/ErrorPages/404.html</patch:attribute>
15 </setting>
16 <!-- LAYOUT NOT FOUND HANDLER
17 Url of page handling 'Layout not found' errors
18 -->
19 <setting name="LayoutNotFoundUrl">
20 <patch:attribute name="value">/ErrorPages/404.html</patch:attribute>
21 </setting>
22 <!-- ERROR HANDLER
23 Url of page handling generic errors
24 -->
25 <setting name="ErrorPage">
26 <patch:attribute name="value">/ErrorPages/Error.html</patch:attribute>
27 </setting>
28 </settings>
29 </sitecore>
30</configuration>

These settings are already defined in the web.config file and changing them here will have the same effect, but I recommend adding patch config files as it will make your solution easier to update in the future.