Setting custom 404 pages for your SharePoint site

When developing any portal solutions and especially a public site solution – you have to account that content authors will leave dead links behind. As a developer – you will probably want to account for that by placing proper 404 page – rather then one that comes as default in the viewers browsers. In SharePoint, setting up 404 page is not handled by using IIS setting.

You will have to use a web application setting to make your SharePint site show custom 404 pages.

Here is what’s involved:

1. On your SharePoint server, navigate to (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\[language ID]) if you’re in multilingual environment you will replace the language ID with the appropriate ID – for English US – it’s 1033.

2. Locate existing sps404.html and make a copy of it; give it a new name (ex my404.html).

3. At the bottom of your newly copied my404.html locate the following string :

STSNavigate("/_layouts/spsredirect.aspx?oldUrl=" + requestedUrl);

and replace the value of  _layouts/spsredirect.aspx to be a site collection relative Url of any page you want to be your 404 page (for example: /Pages/404.aspx)

4. If you’re i multilingual environment  – execute steps 2-3 fr each folder for the language ID.

5. Now that you have created the redirect – you still have to make SharePoint aware of the new 404 page to be used. As mentioned above, it’s a web application setting – but you can’t access it using UI. You can either write a console application or a feature with a feature receiver to get the new value assigned as 404 page. Whichever you choose, here is the code that will do all the legwork (in this case I’m using a feature receiver):

SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;
webapp.FileNotFoundPage = "cp404.html";
webapp.Update();

Good Luck!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , , . Bookmark the permalink.

Comments are closed.