Siyah�n Sadeli�i...

Converted by MaidZoe

IIS 6.0 does not support URL Rewriting, directly. But writing your own HttpHandler, you specify meanful URLs for your web application. That feature gives much more information to Search Engines ( especially Google ) about your page.

Sample:
www.domain.com/[url-info]/[id].[extension]
www.domain.com/iis-6-url-rewriting/445.article

In the scenario, we publish articles from the our pages. And we do not want URLs ( like .../default.aspx?id=445 ).

Default.aspx

public void Page_Load(object sender,System.EventArgs e)
{
string sId = Request.QueryString("id");

//Connect database and Get Data with sID
// We just simply write sId on the page

Response.Write(sId);
}

and then we implement IHttpHandlerFactory interface

public class myHandler : IHttpHandlerFactory
{
...


public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{

int s1 = context.Request.RawUrl.LastIndexOf("/");
int s2 = context.Request.RawUrl.LastIndexOf(".");

// we find id section of the requested URL ( http://www.domain.com/non-existed-folder/302.article )
string sID = context.Request.RawUrl.Substring(s1 + 1, s2 - s1 -1);


//Then rewrite the URL Path of the context
context.RewritePath(context.Request.RawUrl + string.Format("?id={0}", sID));


//Call the page which execute the code by using sID
return PageParser.GetCompiledPageInstance("~/default.aspx", pathTranslated, context);
}
}




Web.Config


IIS Settings
Open properties window of your application. From configuration section, add new extension which is be handled by aspnet_isapi.dll



download the source