Navigate Up
Sign In

How we did it: 301 Redirects in IIS 7.5 on Windows Server 2008 R2

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

​NothingButSharePoint.com went live in December 2010 after a huge effort by everyone involved. I had migrated all the content from EndUserSharePoint.com (WordPress) and SharePointDevWiki.com (Atlassian Confluence). We decided to leave the old systems running in read-only mode with a big note at the top of each page to inform users that we'd moved.

What we noticed was that when we did searches in Google/Bing on articles that were prior to launch, the old site would be ranked highly, but the migrated content wouldn't. This was something I had been looking into a solution for for a long time. I had been recommended that the best way is to fire 301 Redirect messages when the old domain url was requested.

IIS 7.5 has an add-on module called URL Rewrite. This allows you not only to do one to one mappings between old urls and new urls, but also to do things such as wildcard mappings using Regular Expressions to pattern match. The Using the URL Rewrite Module is a great getting started guide.

One thing to take into account is that SharePoint has its only HttpModule that handles a lot of URL rewriting to access Sites, Lists and List Item contents. So what I did was created a new IIS Web Application on our Production server, which binded to www.SharePointDevWiki.com. I installed URL Rewrite on the server and then in the web.config for my new Web Application started adding one to one mappings. Below is a snap shot of this:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
		<rule name="rule 1 Pod Casts" patternSyntax="ExactMatch" stopProcessing="true"><match url="display/SPPodCasts/Home" /><action type="Redirect" url="https://www.nothingbutsharepoint.com/sites/devwiki/PodCasts/Pages/Home.aspx" /></rule>
	    <rule name="rule 2 Pod Casts" patternSyntax="ExactMatch" stopProcessing="true"><match url="display/SPPodCasts/2010/04/17/SPWebCast+011+-+SharePoint+2007+and+HP+TRIM+7+Integration+Overview" /><action type="Redirect" url="https://www.nothingbutsharepoint.com/sites/devwiki/PodCasts/Pages/SPWebCast 011 - SharePoint 2007 and HP TRIM 7 Integration Overview.aspx" /></rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
    </system.webServer>
</configuration>

 


URL encoding characters

Unfortunately some of the URLs encoding characters were not supported by IIS out of the box so I had to tweak a setting to allow the plus sign ('+') by changing the Web Application IIS Metabase:


%windir%\system32\inetsrv\appcmd set config "SharePointDevWiki.com Redirect" -section:system.webServer/security/requestfiltering -allowDoubleEscaping:true

 


Obtaining the mappings

 

So as much as I would have loved to go through every article and find the new equivalent URL, I was lucky enough to notice that the MetaLogix tool stored the original source URL as a metadata Site Column on the List Item. So I wrote some nifty PowerShell to generate the entries for the web.config.


Add-PSSnapin Microsoft.SharePoint.Powershell 

Function Write-XMLURLRedirect ([int] $i, [string] $siteUrl, [string] $webUrl)
{
    $spsite = Get-SPSite -Identity $siteUrl;
	if ($webUrl.Length -eq 0)
	{
		$web = $spsite.OpenWeb();
	}
	else
	{
 	   $web = $spsite.OpenWeb($webUrl);
	}
	  
    $pages = $web.Lists["Pages"].GetItems();
    $pages | ForEach-Object {
        $title = $_.Title
        $url = $_["MigratedSourceURL"].Replace("http://www.sharepointdevwiki.com/", "");
        $newUrl = $web.Url + "/" + $_.Url
        $ruleTitle = $i
        if ($url -ne $null)
        {
            if ($url -ne "")
            {
                "<rule name=`"$ruleTitle`" patternSyntax=`"ExactMatch`" stopProcessing=`"true`"><match url=`"$url`" /><action type=`"Redirect`" url=`"$newUrl`" /></rule>" | Out-File c:\test.txt -Append
                $i = $i + 1;
            }
        }
    }
        
    $web.Close();
    $spsite.Close();
}

    $i=1;
    
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "PodCasts"
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "PlayBook"
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "SP2010Dev"
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "SP2007Dev"
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "SP2007Admin"
Write-XMLURLRedirect $i "https://www.nothingbutsharepoint.com/sites/devwiki/" "SP2010Admin"

I have a $i parameter which gives each of my entries a unique number, as the Name can't be the same.

 

Web.config size restrictions

In Microsoft's wisdom, there is a 250Kb limitation set in IIS server level for the maximum size of the web.config. Now with EndUserSharePoint.com we actually had over 2,500 articles migrated and the size of the web.config was over the limit. You can workaround this by changing the registry on the machine, as SharePoint runs on a 64-bit server, the 32-bit IIS is looking for isn't there...so you have to add the container and then the key in regedit.com:

 

 

 


HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Configuration
"MaxWebConfigFileSizeInKB"=dword:000007d0

 


Testing

Before switching this live, to test this I simply changed my local host file to point the old domains to the new Production server. I used Google Search's 'site:' property to test the first few pages of URLs and they all worked a treat when they were articles. I had forgotten a few mappings such as the base homepages for each area which I added. I noticed immediately that I needed to add one for the base url, unfortunately I could not add a mapping in the web.config. So I added a default.aspx page to the new Web Application and did a 301 Redirect in inline managed code as below:


<%@ Page Language="C#" %>
<% Response.Redirect("https://www.nothingbutsharepoint.com/sites/devwiki/", false);
Response.StatusCode = 301; 
Response.End();
%>

There are enormous benefits to these redirects. Anybody who has referenced our original articles will now redirect to our new site and users clicking on their blogs will be aware of the move immediately. Because I have used 301 Redirect, Google will actually recognise that NothingButSharePoint.com is now the primary site and start raising our ranking.

I'd like to give a big shout out to Garth O'Brien (@GarthOBrien) who is an MVP Lead and Search Engine Optimisation expert. He was the one that kicked my concerns around SEO at TechEd North America last week with his presentation on SEO and "Google Juice". I will be writing more articles in the future around how I improve SharePoint 2010 to increase our sites SEO as out of the box SharePoint fails a few golden rules :-(

In terms of the effect it has had on Google since switching this on Monday night, a picture paints a thousand words!

Google Analytics Chart

We haven't switched End User SharePoint over yet because the Stump The Panel forums are still hosted on a Apache server on that url. So I need to use Apache rewriting technology for that. Luckily I can just tweak my PowerShell to generate the syntax for Apache...result! Did I say already how much I love PowerShell ;-)

 

 

 

 

Categories: Deployment; Performance and Optimization; PowerShell

Comments

stained glass melbourne

How we did it: 301 Redirects in IIS 7.5 on Windows Server 2008 R2

The post is pretty interesting. I really never thought I could have a good read by this time until I found out this site. I am grateful for the information given. Thank you for being so generous enough to have shared your knowledge with us.

Posted 19-Dec-2011 by stained glass melbourne

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: