You will need to have a custom processor in the <httpRequestBegin>
which see if the item in the url is valid or not.
So, the patch will be as follows:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<httpRequestBegin>
<processor type="YourNamespaceHere, YourAssemblyHere"
patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>
Now, the code will be as below:
public class NotFoundProcessor : HttpRequestProcessor
{
#region Fields
private static readonly List<string> UrlsToIgnoreByCustomProcessors = new List<string> { "/sitecore/", "/sitecore", "/layouts", "/layouts/", "/error500.aspx" };
#endregion
public override void Process(HttpRequestArgs args)
{
if (UrlsToIgnoreByCustomProcessors.Any())
{
foreach (var urlsToIgnore in UrlsToIgnoreByCustomProcessors)
{
if (HttpContext.Current.Request.Url.PathAndQuery.ToLower().Contains(urlsToIgnore))
{
return;
}
}
}
//Check if Context Item is null
if(Sitecore.Context.Item != null)
{
return;
}
//Get Path of Error 404 item from Site Definition
var error404ItemPath = Sitecore.Context.Site.Properties["notFoundPageItemPath"];
var database = GetDatabase();
//Get Error 404 item from Sitecore
var error404Item = database.GetItem(error404ItemPath);
//Generate url for Error 404 item
var itemUrl = LinkManager.GetItemUrl(error404Item).Replace(context.DomainName, string.Empty);
//Perform Redirection
HttpContext.Current.Server.TransferRequest(string.Format("~/{0}?status=404", itemUrl.TrimStart('/')));
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
protected virtual Database GetDatabase()
{
return Context.ContentDatabase ?? Context.Database;
}
}