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(https://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: https://support.microsoft.com/kb/218155
* This seems to be working within a site collection context only, i.e. https://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
Anonymous
April 08, 2007
PingBack from http://stevepietrekweblog.wordpress.com/2007/04/08/links-482007/Anonymous
April 08, 2007
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.aspxAnonymous
April 10, 2007
Jingmei Li provides nice solution how to create your own custom 404 error page and handle redirect inAnonymous
April 12, 2007
The comment has been removedAnonymous
May 11, 2007
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.Anonymous
July 12, 2007
This post is about how to create custom landing pages ( www.mysite.com/products or www.mysite.com/newsletterAnonymous
July 25, 2007
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.Anonymous
December 07, 2007
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.Anonymous
December 21, 2007
Body: You can define a custom error page for SharePoint (see ... Category: SharePoint Published: 9/04Anonymous
December 28, 2007
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? ThanksAnonymous
October 22, 2008
Link Integrity Management: Processes, Tactics, and Solutions One lengthy and complex topic I recentlyAnonymous
October 29, 2008
The comment has been removedAnonymous
June 03, 2009
Good post and very much usefulAnonymous
June 08, 2009
This post is about how to create custom landing pages ( www.mysite.com/products or www.mysite.com/newsletterAnonymous
August 03, 2009
An easier approach for a MOSS Internet web site would be:
- Create a new error page using any page layout say WebError.aspx (http://www.yourapplication/Pages/WebError.aspx)
- Modify web.config and set the error redirect page URL to the above Page.
- Add code to Application_Error event in global.asax for additional error handling. --Nauman.
- Anonymous
February 02, 2010
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?