SharePoint Vanity URL Rewriting
During the day I am a software engineer and from time to time I tend to come up with some cool solutions that I think other developers would be interested in. So going forward I will be posting some code that I have built and want to share with all you other nerds out there. Here goes!
Looking around the internet I’ve seen that many people have been trying to implement URL rewriting in a MOSS environment. Ive implemented url redirecting with MOSS using the HTTP module route for a while now and I’ve documented the code I used and what parameters worked the best for me here;
Take a look and let me know if this helps you and if you have any questions.
After messing around for a little bit it, I came up with a good way to do it. When I was looking for examples on the web there were a lot of people saying that it couldn’t be done. But in the end it actually didn’t take much to implement it. Here’s an HttpModule that I wrote to do the work.
The key pieces are the this.app.BeginRequest += new EventHandler(app_BeginRequest) which steps in front of the request and allows the module to get its redirect on. And HttpContext.Current.RewritePath(redirect, false); will push the necessary headers n such forward so that the receiving .aspx page will understand how to correctly post back.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Security.Cryptography;
using System.Configuration;
using System.Threading;
using System.IO;
using System.Security;
using System.Security.Principal;
namespace ScaredPanda
{
public sealed class RewriteHttpModule : IHttpModule
{
HttpApplication app = null;
///
/// Initializes the httpmodule
///
public void Init(HttpApplication httpapp)
{
this.app = httpapp;
this.app.BeginRequest += new EventHandler(app_BeginRequest);
}
public void app_BeginRequest(Object s, EventArgs e)
{
try
{
//determine if the income request is a url that we wish to rewrite.
//in this case we are looking for an extension-less request
string url = HttpContext.Current.Request.RawUrl.Trim();
if (url != string.Empty
&& url != "/"
&& !url.EndsWith("/pages")
&& !url.Contains(".aspx")
&& url.IndexOf("/", 1) == -1)
{
//this will build out the the new url that the user is redirected
//to ie pandas.aspx?pandaID=123
string redirect = ReturnRedirectUrl(url.Replace("/", ""));
//if you do a HttpContext.Current.RewritePath without the 'false' parameter,
//the receiving sharepoint page will not handle post backs correctly
//this is extremely useful in situations where users/admins will be doing a
//'site actions' event
HttpContext.Current.RewritePath(redirect, false);
}
}
catch (Exception ex)
{
//rubbish
}
}
}
}


Nice solution, tried umpteen url rewriting solutions but could never get my head around the regular expressions required!! You’ve missed out the code for the ReturnRedirectUrl function.
Twitter: scaredpanda
says:
Thanks Mark. The ReturnRedirectUrl function looks in the database to find the ID of the entity that is being passed as the ‘vanity url’. For instance if I passed in scaredpanda.com/clint, the ReturnRedirectUrl function would return the ID of clint in the database. You can pretty much do whatever you want there.