Customizing the 404 Redirection in SharePoint

by Chakkaradeep 23. June 2009 10:40

By default, SharePoint takes you to its own 404 NOT FOUND page if you enter an Url which does not exist in the site.

image

It is very easy to customize this behaviour and let SharePoint redirect 404 errors to the page you want to.

The important file that manages this 404 redirection is -

12\TEMPLATE\LAYOUTS\1033\sps404.html

This HTML file controls the redirection

<script language="javascript">
    var requestedUrl = escapeProperly(window.location.href);
    STSNavigate("/_layouts/spsredirect.aspx?oldUrl=" + requestedUrl);
</script>

So, to create your own custom 404 redirection, all you need to do is:

1) Make a copy of sps404.html, for example – mysite404.html - and place it in the same folder 12\TEMPLATE\LAYOUTS\1033\

2) Modify the STSNavigate with your page Url:

STSNavigate("/how-to-join/Pages/default.aspx?oldUrl=" + requestedUrl);

Remember that as long as the Url is valid, your redirection will work

3) You have to now set the mysite404.html as your 404 redirection page for your web application.

You can do this via the SharePoint object model. Instead of writing a console app to do this, it is always better to deploy the 404 redirection using a feature which would set the 404 redirection.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWebApplication webApplication = properties.GetSite().WebApplication;
    webApplication.FileNotFoundPage = "mysite404.html";
    webApplication.Update();
}

Note: The FileNotFound is set to NULL by default

Note: You can *only* set a 404 redirection per web application

Deploy the feature, activate it and your custom 404 redirection is ready to go!

Comments

7/7/2009 5:28:59 PM #

Ian Morrish

For admins who want to do this without deeding a developer www.wssdemo.com/.../...-sharepoint-error-page.aspx

Ian Morrish New Zealand | Reply

11/17/2009 3:43:39 AM #

myro

You can't use
properties.GetSite().WebApplication.. it won't even compile.

If you feature's scope is SPSite, you should use:

SPWebApplication webApplication =  ((SPSite)(properties.Feature.Parent)).WebApplication

myro Italy | Reply

11/17/2009 7:02:05 AM #

chakkaradeep

@myro - GetSite() is my helper method.

chakkaradeep | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author




NZ SharePoint Conference 2010

 

 

New Zealand SharePoint User Group