Guidance on returning a 404 Page Not Found response in Sitecore when an item lacks a language version

Posted 10 Aug 2023 by Marek Musielak

return page not found response if sitecore item has no language version

I posted this article on Cognifide blog in 2011 initially. Since Cognifide blog is no longer online (you can still find it in Wayback Machine http://www.cognifide.com/blogs/sitecore/sitecore-displaying-pages-without-a-language-version/), I decided to post it on my personal blog, so this knowledge is not gone.

Scenario: despite the fact that Sitecore item has no version in a certain language, one can still navigate to the url of that language version and Sitecore returns 200 OK result with pretty much blank page containing only shared fields values.

Solution: check whether current context item has any version in current language. If there is no version, assign null to Sitecore.Context.Item. This will result in 404 Page Not Found response as expected.

Code of the processor:

public class ClearContextItemIfLanguageDoesNotExist : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        // don't change the flow of Experience Editor or Preview mode
        if (!Context.PageMode.IsNormal)
            return;

        if (Context.Item != null 
            && Context.Item.Paths.IsContentItem
            && Context.Item.Versions.Count == 0)
        {
            Log.Info($"Not existing language '{Context.Item.Language}' request for item '{Context.Item.Paths.FullPath}'", this);
            Context.Item = null;
        }
    }
}

Config patch file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore role:require="ContentDelivery or Standalone">
        <pipelines>
            <httpRequestBegin>
                <processor 
                    type="MyAssembly.MyNamespace.ClearContextItemIfLanguageDoesNotExist, MyAssembly.MyNamespace"
                    patch:before="processor[@type='Sitecore.Mvc.Pipelines.HttpRequest.TransferRoutedRequest, Sitecore.Mvc']" />
            </httpRequestBegin>
        </pipelines>
    </sitecore>
</configuration>

Comments? Find me on or Sitecore Chat