Wednesday, November 9, 2011
IIS7 Managed Module - URL redirection, Http2Https, File extension re-mapping
As I really want to explore more of the development enhancements that can be done on top of the existing features on IIS7, I wrote a simple Module in C# for IIS7. Since I wanted to write a managed code, I used the ASP.NET server extensibility APIs. Previously, the Modules written in ASP.NET will only be used on the ASP.NET applications. You simply can't do use them on for example static files. But IIS7 allows you to develop your Module and plug it in the main IIS request processing pipeline - Integrated pipeline architecture. Just a powerful example is the Forms Authentication module can be used even for the static file types on IIS7. Amazing isn't it?
Okay, lets now talk about the Module I have developed. This is just a simple module which does three functions:
1. URL Redirection
Example : You want to redirect the request for the URL http://servername tohttp://www.live.com or you want to redirect the request forhttp://www.your_website.com to http://www.your_website.com/email
2. Http2Https redirection
You want to redirect all the http traffic for the site to https
3. File extension re-mapping
You want to redirect all .asp requests as .aspx - automatic file re-mapping or you want to redirect all the requests for the file /file.asp to /file/one.asp
So, as you understand I have 3 scenarios and I also wanted to give the user an option of configuring the URLs himself. Yes you guessed it right. I am going to use an XML file as an input and I just choose the following format:
<redirects>
<url old="http://mywebsite.com/" new="http://mywebsite.com/mail/" />
<http2https enabled="true" />
<fileext old=".asp" new=".aspx" />
<fileext old="file.asp" new="filevidr/file.asp" />
</redirects>
So, how to build a managed module for IIS7? First download and Install IIS7 Managed Module Starter Kit for Visual Studio. It will make your life easier in creating modules for IIS7.
Following is my Init method:
public void Init(HttpApplication application)
{
// register for the BeginRequest event;
application.BeginRequest += new EventHandler(OnBeginRequest);
}
I am subscribing my Module to the BeginRequest event and providing a handler function for the same. Below is the code of my OnBeginRequest function which does all the functions explained about this little module - rewrites URLs, Http2Https, Fileext re-mapping:
public void OnBeginRequest(Object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string HttpUrl = app.Request.Url.ToString();
string filename = "c:\\inetpub\\wwwroot\\redirs.xml";
XmlTextReader tr = new XmlTextReader(filename);
string oldURL = HttpUrl;
string newURL = HttpUrl;
while (tr.Read())
{
// URL Redirection
if (tr.LocalName.CompareTo("url") == 0)
{
oldURL = tr.GetAttribute("old");
if (HttpUrl.Equals(oldURL))
{
newURL = tr.GetAttribute("new");
app.Response.Redirect(newURL.ToString(), true);
app.Response.End();
break;
}
}
//HTTP to HTTPS redirection
if (tr.LocalName.CompareTo("http2https") == 0)
{
if (tr.GetAttribute("enabled").Equals("true"))
{
if (HttpUrl.StartsWith("http:"))
{
newURL = HttpUrl.Replace("http:", "https:");
app.Response.Redirect(newURL.ToString(), true);
app.Response.End();
break;
}
}
}
// File remapping
if (tr.LocalName.CompareTo("fileext") == 0)
{
if (HttpUrl.EndsWith(tr.GetAttribute("old")))
{
newURL = HttpUrl.Replace(tr.GetAttribute("old"), tr.GetAttribute("new"));
app.Response.Redirect(newURL.ToString(), true);
app.Response.End();
break;
}
}
}
tr.Close();
}
I read from the XML file using XmlTextReader and getting the values and redirecting the HttpApplication context to the modified URL.
To know how to build / deploy check Developing a Module using .NET article in IIS.net.
I have also wrote sample ISAPI filter redirecting the requests for .aspx to .asp here and writing a simple ISAPI file here.