public static void FileNotFoundHandler(object sender, EventArgs evt)
{
// Check if this should be enabled
if (Configuration.Configuration.FileNotFoundHandlerMode == FileNotFoundMode.Off)
return;
HttpContext context = GetContext();
if (context == null)
return;
if (context.Response.StatusCode != 404)
return;
// If we're only doing this for remote users, we need to test for local host
if (Configuration.Configuration.FileNotFoundHandlerMode == FileNotFoundMode.RemoteOnly)
{
// Determine if we're on localhost
bool localHost = IsLocalhost();
if (localHost)
{
Logger.Debug("Determined to be localhost, returning");
return;
}
Logger.Debug("Not localhost, handling error");
}
Logger.Debug("FileNotFoundHandler called");
Uri notFoundUri = context.Request.Url;
// Skip resource files
if (IsResourceFile(notFoundUri))
return;
string query = context.Request.ServerVariables["QUERY_STRING"];
// avoid duplicate log entries
if ((query != null) && query.StartsWith("404;"))
{
return;
}
CustomRedirect newUrl;
var canHandleRedirect = HandleRequest(GetReferer(context.Request.UrlReferrer), notFoundUri, out newUrl);
if (canHandleRedirect && newUrl.State == (int)DataStoreHandler.State.Saved)
{
context.Response.Clear();
context.Response.TrySkipIisCustomErrors = true;
context.Server.ClearError();
context.Response.RedirectPermanent(newUrl.NewUrl);
context.Response.End();
}
else if (canHandleRedirect && newUrl.State == (int)DataStoreHandler.State.Deleted)
{
SetStatusCodeAndShow404(context, 410);
}
else
{
SetStatusCodeAndShow404(context, 404);
}
}