Chaks' Corner

SharePoint and other stuffs

23. June 2009 10:40
by Chakkaradeep
3 Comments

Customizing the 404 Redirection in SharePoint

23. June 2009 10:40 by Chakkaradeep | 3 Comments

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 (3) -

Ian Morrish

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

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

chakkaradeep

@myro - GetSite() is my helper method.

Comments are closed