Welcome to MSDN Blogs Sign in | Join | Help

Jingmei Li's MSDN Blog

A place to share out tips and solutions about SharePoint, .NET, etc.
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.

Posted: Sunday, April 08, 2007 10:05 AM by jingmeili

Comments

imorrish said:

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

# April 9, 2007 1:09 AM

Meron Fridman's Blog said:

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

# April 10, 2007 3:54 PM

joelo said:

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.

# April 13, 2007 12:53 AM

bernadou said:

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.

# May 11, 2007 4:35 PM

Michael Hofer - SharePoint Blog said:

This post is about how to create custom landing pages ( www.mysite.com/products or www.mysite.com/newsletter

# July 12, 2007 10:03 AM

JamCo said:

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.

# July 25, 2007 3:03 PM

lenc said:

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.

# December 7, 2007 12:41 PM

Mirrored Blogs said:

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

# December 22, 2007 3:10 AM

RobRoach said:

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

# December 28, 2007 12:41 PM

Brett's SharePoint Blog said:

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

# October 22, 2008 4:22 PM

AllTheGoodNamesAreInUse said:

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.

# October 29, 2008 9:10 AM

Gangadhar Kotu said:

Good post and very much useful

# June 3, 2009 9:25 AM

Sourav Dutta's SharePoint - All Rounder said:

This post is about how to create custom landing pages ( www.mysite.com/products or www.mysite.com/newsletter

# June 8, 2009 6:04 PM
Anonymous comments are disabled
Page view tracker