In case if you are interested in creating your own custom error page and custom access denied page instead of the out of the box MOSS / Sharepoint error page (generally the requirements in Publishing internet site). You can use the following HTTPModule (this is just a sample, you can change it to suit your requirements):

Note to have following Web.config Entries:

Following entries in the <appsettings> section in the web.config file:

  1. Following entry is for a custom error page to be displayed for anonymous users, the key should be in the format: “CustomAnonCustErrPG-SiteCollectionURL” as shown below:

<add key="CustomAnonCustErrPG-http://xyz.com" value="/Pages/default.aspx" />

  1. Following entry is for a custom error page to be displayed for authenticated users, the key should be in the format: “CustomAuthCustErrPG-SiteCollectionURL” as shown below:

<add key="CustomAuthCustErrPG-http:// xyz.com " value="/Pages/default.aspx" />

  1. Following entry is for a custom error page to be displayed for access denied errors, the key should be in the format: “CustomCustAcsDndPG -SiteCollectionURL” as shown below:

<add key="CustomCustAcsDndPG-http://xyz.com" value="/_layouts/XYZ/CustomErrPg.aspx " />

Create an entry on all the WFEs web-application specific web.config in the HTTPModules section, as follows just below the <HTTPModule> tag This should be the first entry in the HTTPModules section:

<add name="XYZExceptionHandler" type="XYZ.Sharepoint.HTTPModule.CustomErrorPages.CustomErrors, XYZ.Sharepoint.HTTPModule.CustomErrorPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=PUBLICKEYGUID" />

NOTE: Add your own Public key for PublicKeyToken per your snk used, replace the PUBLICKEYGUID with your own snk value

// This is a HTTPModule used for redirecting the user to Home or Home Anon site depending on whether 

// s/he is authenticated or anonymous.

//

// The module checks if the url is accessdenied.aspx and redirects the user to proper url. 

////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Configuration;
using Microsoft.SharePoint; 

namespace XYZ.Sharepoint.HTTPModule.CustomErrorPages
{
    public class CustomErrors : IHttpModule 
    {
        #region -- Private fields --
        private static AppSettingsReader m_Reader = new AppSettingsReader();
        private static string m_SiteCollAnonCustomErrPG = "CustomAnonCustErrPG";
        private static string m_SiteCollAuthCustomErrPG = "CustomAuthCustErrPG";
        private static string m_SiteCollCustAcsDndPG = "CustomCustAcsDndPG";        
        #endregion 

        public void Init(HttpApplication context)
        {
            //Attaching event handlers for access denied and error respectively
            //context.BeginRequest += new EventHandler(context_AcessDenied);
            context.EndRequest += new EventHandler(context_AcessDenied);
            context.Error += new EventHandler(context_Error);
        }

        void context_AcessDenied(object sender, EventArgs e)
        {
            try
            {
                HttpApplication httpApp = sender as HttpApplication;
                HttpContext context = httpApp.Context;

                string httpUrl = context.Request.Url.ToString();
                string strCustomAcssDndURL = string.Empty;
                string strSiteURL = string.Empty; 

                if (httpUrl.ToLower().Contains("/_layouts/accessdenied.aspx"))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(httpUrl))
                        {
                            strSiteURL = site.Url;
                        }
                    });

                    HttpContext.Current.Server.ClearError();
                    HttpContext.Current.Response.Clear();
                    strCustomAcssDndURL = getAppSettingsValue(m_SiteCollCustAcsDndPG + "-" + strSiteURL);
                    HttpContext.Current.Response.Redirect(strCustomAcssDndURL, false);
                }
            }
            catch (Exception ex)
            {
                //Do your exception handling
            }
          }
 
        void context_Error(object sender, EventArgs e)
        {
            try
            {
                string strURL = string.Empty;
                string strSiteURL = SPContext.Current.Site.Url;
                Exception[] unhandledExceptions = HttpContext.Current.AllErrors;
                //logging all the exceptions thru the Exception policy file
                foreach (Exception ex in unhandledExceptions)
                {
                //Do your exception handling
                }
 
                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Response.Clear();
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    strURL = getAppSettingsValue(m_SiteCollAuthCustomErrPG + "-" + strSiteURL);
                    HttpContext.Current.Response.Redirect(strURL, false);
                }
                else
                {
                    strURL = getAppSettingsValue(m_SiteCollAnonCustomErrPG + "-" + strSiteURL);
                    HttpContext.Current.Response.Redirect(strURL, false);
                }
            }
            catch (Exception ex)
            {
                //Do your exception handling
            }
        }
         
        public void Dispose()
        {
        }
 
        //Method used to read Appsettings value from web.config for the webapplication
        private static string getAppSettingsValue(string sKey)
        {
            string sValue = null;
 
            try
            {
                sValue = m_Reader.GetValue(sKey, typeof(string)) as string;
            }
            catch (Exception ex)
            {
                                //Do your exception handling
            }
 
            return sValue;
        }
    }
}
转载:http://blogs.msdn.com/ketaanhs/archive/2009/03/16/moss-sharepoint-2007-custom-error-page-and-access-denied-page.aspx