What is URL Rewriting and Why is it needed?
URL Rewriting is a means or a mechanism for a site to provide the desired page, requested by the user in a convenient form which does not correspond to physical structure on the site.
e.g. User may request for a page http://www.product.com/Books.aspx while in reality, as per the structure in your site, you may be expecting http://www.product.com?Products.aspx?category=Books.
The reason for this variation could be : a.) Because your site has done some movements or shuffling of the items b) Or to make the URLs short or more understandable to your users c) To improve your hits on search engine and indexing by crawlers too.
How to do it?
Various ways that one can go about and do it.
1. Write a ISAPI filter, which would do the URL rewriting task
2. Write a HTTP Handler/Module to do the URL rewriting.
3. Use the URLMAPPING feature of ASP.NET 2.0
URLMAPPING feature is a neat feature of ASP.NET 2.0 in which we could specify the incoming URL and the URL to be mapped to, in the web configuration file.
Ofcourse there are other hassles that one would need to take care, irrespective of the approach:
To circumvent this issue, one can use the ~syntax to reference files from the root of the application for example
Hmm…Problem…. No worries. Every problem has a solution and some have more than one.
Also if the user requests http://www.MyBlogs/2009/11/ then IIS would try to search for this folder with a default file in it. It may not be feasible or rather cumbersome to create a directory for each year and each month with default file.
IIS 7 specifies a way by which all requests will be directed to an HTTP module.
URL Rewriting in HTTP Modules
URL Rewriting can also be achieved via HTTP Modules and HTTP handlers.
HTTP modules respond to the specific events raised by HTTPApplication like Begin Request, Authenticate Request, Authorize Request, etc. Modules can be configured to respond to any of the events raised by the application before the request is transferred to the Handlers.
Now changing the URL can be done by creating a custom HTTP Module which handles the desired event(s) and uses the RewritePath() method of HttpContext class to rewrite the URL in the request object. Seems Simple… not really.. Changing the URL in the Request object may have an impact on the processing carried out by the existing modules provided by ASP.Net e.g.( FormsAuthenticationModule, FileAuthorizationModule, UrlAuthorizationModule). Example, if URL Rewriting is done in AuthorizeRequest Event, FileAuthorization module would not work as the requested URL would not have been replaced till then and hence FileAuthorization would be working on an incorrect file in the URL and would mostly not object.
If you are using forms authentication and are not using Windows authentication, place the URL rewriting in the AuthorizeRequest event handler. Finally, if you are using Windows authentication, schedule the URL rewriting during the BeginRequest or AuthenticateRequest events.
URL Rewriting can be accomplished via HTTP Handler or HTTP Handler factory. HTTP handler is the class responsible for generating specific content for a type of request. To perform URL rewriting through an HTTP handler, we can create an HTTP handler factory whose GetHandler() method checks the requested path to determine if it needs to be rewritten. If it does, it can call the passed-in HttpContext object's RewritePath() method, as discussed earlier. Finally, the HTTP handler factory can return the HTTP handler returned by the System.Web.UI.PageParser class's GetCompiledPageInstance() method. (This is the same technique by which the built-in ASP.NET Web page HTTP handler factory, PageHandlerFactory, works.)Since all the HTTP modules are initialized before HTTP handlers, the FileAuthorizationModule will be triggered earlier to the URL being rewritten. So if Windows Authetication and file authorization are being used, one will have to use the HTTP Modules approach for URL Rewriting.In both the approaches that is HTTP Modules and HTTP Handler, the rules for rewriting could be stated in the Configuration file i.e. the pattern to look for and the string to replace could be stated in the Configuration file. This would make the application more susceptible to changes in the rules for rewriting. Also the pattern to look for could be expressed easily using regular expressions.
3. List of some good search engine optimization suggestions
a. SSW Rules to Better Google Rankings
b. how URLs can affect top search engine ranking.
4. Already built HttpModules available on the web for free URL Rewriting
5. ISAPIRewrite to enable Extension-less URL Rewriting for IIS5 and IIS6
a. Helicon Tech's ISAPI Rewrite: ISAPI Rewrite full product version for $99
b. Ionic's ISAPI Rewrite: Free download (both source and binary)