Website Performance

Document Sample
Website Performance Powered By Docstoc
					                                        SUVG Presentation

       301 Redirects in Sitecore using

             Presenters: Nick Laidlaw
               and Douglas Couto

10/4/2011                   1
                                               SUVG Presentation

                301 Redirects are
• Important
• Useful for
      – users with bookmarks to old pages
      – banner ads to old pages
• Defined in the HTTP Status Code Definitions
• Easy to do with Sitecore and Lucene

10/4/2011                          2
                                                      SUVG Presentation

               Real world example
• Problem
      – Around 300 URLs to redirect
      – Most URLs were CFM
      – Most URLs contained query string parameters
• Solution
      – Automate the process of creating the items in Sitecore
      – Store items in auto-generated folders to avoid more
        than 100 items in a folder
      – Index and search items using Lucene

10/4/2011                                 3
                                                                   SUVG Presentation

                  Real world example
                Solution Implementation
• Data import
      – Sitecore Architecture
            • New 301 Redirect template with two fields: “Old Url”, single-
              line text field, and “New Item”, general link field
      – Xenu
            • Provided a list of URLs that we used as a basis to import into
      – Microsoft Excel
            • Used filtering and sorting functionality to remove
              unnecessary URLs.

10/4/2011                                              4
                                         SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• Xenu

10/4/2011                    5
                                                                  SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• Data import
      – SQL Server
            • Imported the list of URLs generated by Xenu into SQL Server
            • One table with 4 columns: ID, Address, DateCreated,
      – C# Import Utility
            • C# script that uses the Sitecore API to create the 301 redirect
              items and set the value of the “Old Url” field in each item to
              the value coming from the “Address” field in SQL Server
            • “Clean” URL to use as Item name

10/4/2011                                             6
                                                                                    SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• C# Import Utility
//search for an existing 301 Redirect item in Sitecore with the current "old url"
bool redirectItemAlreadyExists = false;

SafeDictionary<string> refinements = new SafeDictionary<string>();

//this escape is necessary for the Lucene query.
refinements.Add("Old Url", s.Replace("?", "\\?"));

using (var searcher = new Searcher("301_redirects")) {
    List<SkinnyItem> results = searcher.GetItems(new FieldValueSearchParam {
        Language = Sitecore.Context.Language.Name,
        Refinements = refinements,
        ShowAllVersions = false

    redirectItemAlreadyExists = results.Count > 0;

10/4/2011                                                               7
                                                                                          SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• C# Import Utility
//defining a list of safe characters for a Sitecore Item name
List<char> alphaNumeric = new List<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z' };

foreach (string s in oldUrls) {
    //loop through all of the characters in the "s" string variable replacing any non-alpha-numeric
character with a "-"
    char[] safeChars = new char[s.Length];

    for (int i = 0; i < s.Length; i++) {
        if (alphaNumeric.Contains(s.ToLower()[i])) {
            safeChars[i] = s[i];
        else {
            safeChars[i] = '-';

    string safeName = new string(safeChars);

10/4/2011                                                                         8
                                                                                                  SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• C# Import Utility
// {EC0299A1-22FD-4A47-AA68-7B91A4616963} - template ID of template "301 Redirect"
newRedirect = RedirectsFolder.Add(safeName, new TemplateID(new ID("{EC0299A1-22FD-4A47-AA68-7B91A4616963}")));

//set the folderName to be the second character in the ID of the newRedirect item that was just added to the
RedirectsFolder in the line above
//the folderName will be a hexadecimal value: any digit or any letter A - F
string folderName = newRedirect.ID.ToString().Substring(1, 1);

//find the folder by the folderName defined above
Item destinationFolder = RedirectsFolder.GetChildren().Where(i => i.Name == folderName).FirstOrDefault();

//the folder was not found, it must not exist. create it
if (destinationFolder == null) {
    // {D114514E-B4BA-48CB-BFF4-1913F5F559E7} - template ID of template "301 Redirects Folder"
    destinationFolder = RedirectsFolder.Add(folderName, new TemplateID(new ID("{D114514E-B4BA-48CB-BFF4-

//move the newRedirect item to the destinationFolder

//set the value of the "Old Url" field

newRedirect.Fields["Old Url"].Value = s;

10/4/2011                                                                               9
                                                                            SUVG Presentation

                  Real world example
            Solution Implementation cont’d
• Lucene
      – New index specifically for items of template “301 Redirect”.
• Pipeline Processor
      –     Find redirect item using FieldValueSearchParam on “Old Url” field
      –     Add “Status” response header with string “301 Moved Permanently”
      –     Add “Location” response header with URL from “New Item” field
      –     End response to force browser to redirect immediately
      –     No need to use Response.Redirect()
• Web.config
<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel" />
<processor type="_301Redirects.Processor, 301Redirects" />
<processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel" />

10/4/2011                                                      10
                                            SUVG Presentation

• Performance
      – Lucene indexing
• Scalability
      – Auto generation of folders
• Portability
      – Reusable code for other projects

10/4/2011                      11

Shared By:
Tags: redirect
Description: 301 redirect it means a variety of methods to a variety of network requests the re-direction to another location. Such as: Web page redirection.