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