user446923
user446923

Reputation: 553

Sitecore custom 404 redirect

I am using the following implementation to handle 404 in a sitecore multisite solution:

In custom implementation of ExecuteRequest pipeline,

protected override void RedirectOnItemNotFound(string url)
        {
            var context = HttpContext.Current;

            try
            {
                // Request the NotFound page
                var domain = context.Request.Url.GetComponents(
                    UriComponents.Scheme | UriComponents.Host,
                    UriFormat.Unescaped);

                string content;

                using(var webClient = new WebClient())
                {
                    content = webClient
                        .DownloadString(string.Concat(domain, url));
                }

                // The line below is required for IIS 7.5 hosted 
                // sites or else IIS is gonna display default 404 page
                context.Response.TrySkipIisCustomErrors = true;
                context.Response.StatusCode = 404;
                context.Response.Write(content);
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("Failed on URL: {0}. Falling back to default redirection behaviour. Reason for error {1}", url, ex), ex);

                // Fall back to default behavior on exceptions
                base.RedirectOnItemNotFound(url);
            }

            context.Response.End();
        }

Now as you can see, if a 404 page is not found (i.e. there's no 404 page (as defined in Sitecore's ItemNotFound setting in web.config for a site) then I am doing base.RedirectOnItemNotFound which tries to throw 404 effectively coming back to my custom 404 handler again and thus it falls into a redirect loop.

So if someone forgets to add a 404 page to one of the sites then it brings down all other sites and goes into a stalemate.

My question is, what would be the best way to handle this scenario where one of the sites doesn't have a 404 page?

Re-throw the exception instead of base.Redirect..?

Cheers

Upvotes: 1

Views: 3747

Answers (2)

Varun Shringarpure
Varun Shringarpure

Reputation: 302

I think in case if the site specific Error Page is not set, the best thing would be to redirect it to basic Sitecore notfound page, with url /sitecore/service/notfound.aspx

A better thing of doing this would be in IIS Manager itself. The steps are as follows:

  1. Goto IIS Manager, select site in the left pane an select Error pages module.
  2. Select 404 Error page, click on edit in Actions pane.
  3. Select Execute URL and specify URL to be /sitecore/service/notfound.aspx
  4. Again, in Action pane, click Edit Feature Settings, and make sure that it is set to Custom error page. (which I think it should be, as in your case you already have custom error pages)

As per my knowledge, this should solve your problem.

Do let me know what happens. If you need to see how to do changes for Error page in IIS Manager please refer to a similar kind of question that I had answered some time back (Sitecore - Handle 500 errors separately per site in multisite setup)

Hope this input helps you!

Regards, Varun Shringarpure

Upvotes: 0

user459491
user459491

Reputation:

In your scenario I suggest you to have a global 404 page for all sites.

If you don't have on a site 404 page you have to show content from that global page.

When you create sites on Sitecore structure you can create with Branch Templates and you can add 404 page for all sites.

Also you can fix security for every 404 page for all sites. Just some special roles ( admins) will delete 404 page .

Upvotes: 1

Related Questions