Tuesday, 28 June 2011

Sharepoint redirect 301

Background
My company has just been taken over, and one of the requirements was to re-brand the website and move onto a different domain. Now any SEO guru will tell you that this will affect your google rankings, and for this migration to be partially successful it's better to use 301 (permanently moved) rather that 302 (temporarily moved) at page level. This means when the good old crawlers come to your page, they should know that they need to start crawling onto your new site (new site collection).

Like all developers do, I searched the web and couldn't find any reasonable solutions, so I thought it would be a great time to start my first blog and share my solution with the world.

Objective
Create a simple and effective method to apply page level redirection (301) to our old public facing sharepoint site.

Approach
To create a custom Assembly which inherits from System.Web.UI.MasterPage and adds the relevant headers to each page.


1.  Open Visual Studio 2008/2010 and create a new class library project.




2.  Add the following code to you class file. Obviously you will be able to make the code a lot smarter, but I've had to comment out a lot of code as it's specific to our environment.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace RedirectHandler
{

    public class MasterPageTemplate : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            //Get the absoloute URL
            string strRequestPath = GetURL().ToLowerInvariant();

            string strNewPath = "";
            //new and old domain names
            string strOldURL = "
http://www.oldurl.com";
            string strNewURL = "
http://www.newurl.com";
            strNewPath = strRequestPath.Replace(strOldURL,strNewURL);
            ////Add to HTTP header
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location", strNewPath);


        }
       
        private string GetURL()
        {
            string strApplicationPath = "";

            //check to see if not null
            if (this.Page.Request.Url != null)
            {
                strApplicationPath = this.Page.Request.Url.AbsoluteUri;
            }

            //return path
            return strApplicationPath;
        }

    }
}


3.  Compile your project and sign the Assembly.

4.  Add your strongly signed dll into the GAC (C:\WINDOWS\assembly). If you have access to the production server, you can do this by draggging the file into the folder.







4.  Add the following to your Web Application web.config file. This registers the assembly as "Safe", remember to change the PublicKeyToken.  To research further into this, click here..

<SafeControl Assembly="RedirectHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" Namespace="RedirectHandler" TypeName="*" Safe="True" />

5.  Restart IIS. Job Done!