1

I have a parent page with many children pages. Some of those children were deleted and I would like to redirect those broken pages that currently go to 404 page back to the parent page.

I am currently using this. Is there a way to get something like home/products/wire to go back to home/products if wire page does not exist?

public override void Process(HttpRequestArgs args)
    {
        if (Sitecore.Context.Database == null)
            return;
        if (string.Equals(disallowedDatabases, Sitecore.Context.Database.Name, StringComparison.InvariantCultureIgnoreCase))
            return;
        if (string.Equals(disallowedSites, Sitecore.Context.Site.SiteInfo.Name, StringComparison.InvariantCultureIgnoreCase))
            return;
        // retain 500 response if previously set
        if (HttpContext.Current.Response.StatusCode >= 500 && HttpContext.Current.Error != null)
        {
            Log.Error("Custom Error : Internal Server Error :  " + HttpContext.Current.Error.InnerException, this);
            return;
        }
        // return if request not equal to value set in ItemNotFoundUrl, i.e. successful page
        if (string.Equals(args.Context.Request.Url.LocalPath, Sitecore.Configuration.Settings.ItemNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
        {
            HttpContext.Current.Response.TrySkipIisCustomErrors = true;
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound;
            HttpContext.Current.Response.StatusDescription = "Page not found";
            Log.Error("Custom Error : Item Not Found :" + args.Context.Request.RawUrl, this);
        }
        else if (string.Equals(args.Context.Request.Url.LocalPath, "/500", StringComparison.InvariantCultureIgnoreCase))
        {
            HttpContext.Current.Response.TrySkipIisCustomErrors = true;
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            HttpContext.Current.Response.StatusDescription = "Internal server Error";
            //Log.Error("Custom Error : Item Not Found :" + args.Context.Request.RawUrl, this);
        }

    }
2
  • It is not a good idea to redirect a 404 to its parent item, better to use the 301/302 redirect for the URLs. – Mahendra Shekhawat May 20 '20 at 8:38
  • Hi Mahendra. Don't you need to know the exact URL though? My goal is not to do these manually. Can 301 redirects be set to see broken pages? – user6919 May 20 '20 at 17:05

Your Answer

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

Browse other questions tagged or ask your own question.