SPS 2003 : Custom error page for a status 404 error

I was recently working on SPS 2003 issue where we wanted to redirect the user to a custom error page. In MOSS we have an elaborate process to achieve the custom error page functionality.

After quite a bit of research I could find couple of ways but the best and most efficient one seems is to use the HTTP Module for the same. Certain important pointers to remember before using the sample code below. Firstly create your own custom page – preferably a layouts or application page in the layouts directory. Secondly the assembly will have to deployed to the GAC folder and add the Custom HTTP Module entry to the web.config. Lastly do an iisreset to ensure everything is reseted before use of http module.

<Sample code>

using System;
using System.Web;
using System.Net;

namespace Inter
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1 : IHttpModule
    {
        public Class1()
        {
        }

        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        public void Dispose()
        {
            // TODO:  Add CustomModule.Dispose implementation
        }

        #endregion

        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication objHttp = (HttpApplication)sender;
            //objHttp.Context.Response.End();                      
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication objHttp = (HttpApplication)sender;

            if (objHttp.Context.Response.StatusCode == 404)
            {
                objHttp.Context.Response.Redirect("https://" + objHttp.Context.Request.Url.Authority + "/default.aspx");
            }
        }
    }

}

</Sample Code>