3

I have a URL, myWebsite.com/locations, and currently if you go to myWebsite.com/locations/{any valid ID} you get a page with content about the location.

However, if you go to myWebsite.com/locations/{anything except a valid id}, instead of a 404 page, you get a page with just the header and footer.

Instead of loading a page with the header and footer I would like it to lead to a 404 page with some content, however I'm struggling to find documentation on how to accomplish this.

  • Is the item under locations a wildcard item? – Gatogordo May 31 '17 at 19:27
  • Correct. there is a wildcard underneath locations – Andrew Diamond May 31 '17 at 19:30
1

The reason why you are seeing an empty page (with header and footer) when requesting a faulty id is probably because the code in the rendering that shows your actual content can't find any data and therefor is not showing anything.

I see two possible solutions:

1. Custom processor in <httpRequestBegin>

In this case you need to move some logic to the processor as you will need to define if the id is valid or not. But you don't want to do this again in your rendering code (when it is valid) to get the data - so you might want to try and fetch the data and store it in a context object for the request - reusable for the code that renders the data.

If you detect a bad id, you can handle it as a 404 request (set the context item to a 404 item - how to handle 404's is described in several blogs and also here: How do you setup a 404 and 500 error page for missing files and media items?)

2. Handle it in your rendering on the wildcard item

This approach can work if you have one rendering on the wildcard page that renders it all (or have some central place for that logic at least). When you detect a false id, you could show a 404 instead - you could e.g. render another view and set the response status code. Just make sure not to use a redirect as that will not generate the correct status code (important for search engines).

It's hard to tell which solution is best in your case as we do not know how the code on your wildcard page is structured but this might give you some ideas.

| improve this answer | |
1

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>

Basically, what you need to do is to see first if the request url contains elements which should be neglected. Then, you need to see if the Context.Item is null. If it is null, therefore it should redirect to the Error 404. So, the code will look something 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;
    }
}

You will be able to add presentation to show some content to the Error 404 item.

Note: It is recommended to add caching so that it will not each time go and fetch the item in the Sitecore database. For more information, you may check the following link

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.