How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?


People alway ask how to use their own 404 file not found error page vs. the generic one from IE in MOSS environment. The following example catches the 404 error and sends users to a redirect page.


 


Here’s the steps:


 


1. In your MOSS server, make a copy of


%systemdrive%\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033\sps404.html


and call it my404.html


 


2. Create a Virtual Directory in IIS under your MOSS root web application. For example /errors


3. Create your own redirect aspx page, for example /errors/my404redirect.aspx and code your redirect logic in there. This is a normal asp.net page. 


4. In my404.html, make the following change:


            STSNavigate(“/errors/my404redirect.aspx?oldUrl=” + requestedUrl);


5. Create a Console Application and insert the following code and run it in MOSS server


 


           System.Uri webApplicationUri = new Uri(http://MyMOSSServer/);


           SPWebApplication webApplication = SPWebApplication.Lookup(webApplicationUri);


           webApplication.FileNotFoundPage = “my404.html”;  //*note


           webApplication.Update();


*Note: By default this is set to null. FileNotFoundPage needs to point to a html file that lives in %systemdrive%\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033. The file needs to be html only.


 


6. Now when you browse to a page that doesn’t exist, you should expect to be brought to the redirected page.


 


 *Another note:


In IE there’s a “Show friendly HTTP error messages” setting which is ON by default in Internet Options->Advanced. With this setting on, sometimes your custom error page is not displayed. In order to override this setting, both my404.html and /errors/my404redirect.aspx from the above steps need to be larger than 512 bytes in size. Refer to the following KB about this setting: http://support.microsoft.com/kb/218155 


* This seems to be working within a site collection context only, i.e. http://MyMOSSServer/sites/siteA if sites is a wildcard inclusion managed path and siteA doesn’t exist in MOSS then this URL will NOT trigger the custom 404 error page set to SPWebApplication.FileNotFoundPage property.

Comments (16)

  1. imorrish says:

    Here are the PowerShell caommands I used instead of having to compile a console app (step 5 in Jingmei’s blog).

    PS C:> [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

    PS C:> $webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://www.wssdemo.com")

    PS C:> $webapp.FileNotFoundPage = "wssdemo404.html"

    PS C:> $webapp.Update()

    Demo http://www.wssdemo.com/pages/deletedpage.aspx

  2. Jingmei Li provides nice solution how to create your own custom 404 error page and handle redirect in

  3. joeloleson says:

    There are other tricks to forcing yout custom 404 to show up.  The logic is that the 404 needs enough text to be considered a good 404 page by IE.  Add more text to your 404 and it will come up.

  4. bernadou says:

    Very nice article.  

    I’m curious why one can’t use a .aspx file?  The HTML file contains JS which can easily be turned off, and, so can META refreshes.

    Like:

    webApplication.FileNotFoundPage = "my404.aspx";

    Assuming the file is in the /_layouts VD?

    Seems like it ‘should’ work, but, it doesn’t as you pointed out.

  5. JamCo says:

    imorrish:  Thank you!  I don’t have an extra server license and studio license just sitting around for sharepoint so there is no way to compile a console app.  PowerShell worked awesome!

    I wonder though, why not make an ASPX script that references the assembly and makes the required call to SPWebApplication?  That way you need not compile a console application or install PowerShell.

  6. lenc says:

    Does this work for content not found in the _layouts directory?  For pages not found in _layouts, we either get a sharepoint page saying "Error" and a link to "TroubleShoot issues with SharePoint Services".  We don’t want to show that – it’s an internet facing custom Portal.

  7. Body: You can define a custom error page for SharePoint (see … Category: SharePoint Published: 9/04

  8. roberoach@hotmail.com says:

    In my testing it appears that the webApplication.FileNotFoundPage is set when the web apps top level site is a team site, but is not set (or turned off by publishing?) when the top level site is a publishing site.  Can anyone confirm that this is correct or just my local machine issue?  

    Thanks

  9. Link Integrity Management: Processes, Tactics, and Solutions One lengthy and complex topic I recently

  10. Myyzius says:

    So, I copied the original file to a new one.

    Calle NEW404.html, in the same layouts 1033 as the sps404.html.

    Then I just changed the redirect to point to _layouts/myredirect/myredirectp.asx?oldurl=….

    Then I used powershell to set the value as per the post above.

    Then when I try a nonexistent url, I get a popup window in IE stating "Invalid page url".

    If I use the same powershell to do $webapp.FileNotFoundPage="sps404.html" and then run $webapp.Update(), it works just fine.

    So, if I place my custom code in the sps404 file it work like a charm.

    And if I place the same code in my own file, it does not work.

    Does anyone have any ideas where to look for the issue here?

    I did check the logs and they just show that I have requested a nonexistent url, and in the error case it goes to the moss site frontpage and shows all normal 200 resultant stuff.

    And in the working case I see a 302, and it shows my browser has requested the correct myredirect.aspx stuff.

    Darn.

  11. Gangadhar Kotu says:

    Good post and very much useful

  12. nauman_ahmed says:

    An easier approach for a MOSS Internet web site would be:

    1. Create a new error page using any page layout say WebError.aspx (http://www.yourapplication/Pages/WebError.aspx)

    2. Modify web.config and set the error redirect page URL to the above Page.

    3. Add code to Application_Error event in global.asax for additional error handling.

    –Nauman.

  13. elephantfive says:

    This approach seems to work for me just fine if I’m actually on the server. When I go to a missing page from a different computer, the redirect isn’t triggered. Thoughts?

Skip to main content