Rewriting URLs in ASP.NET on Loopia hosting lets you produce clean, search-engine-friendly addresses (often called “pretty URLs”) without relying on Apache’s mod_rewrite. This article explains how to use UrlRewriter.NET on our Windows/IIS platform to achieve the same result that mod_rewrite provides on UNIX servers.
Why use URL rewriting in ASP.NET?
Using so-called “pretty URLs” has become increasingly common as a way to improve the usability of web applications. It may also provide some benefit to a website’s chances of a good placement in search engines.
On our UNIX servers, it is very easy to rewrite URLs via mod_rewrite, but those who prefer Windows and ASP.NET do not have the same opportunity to use mod_rewrite. However, there is a clean solution that is simple and does not require any additional installation on the server: UrlRewriter.NET.
How to use UrlRewriter.NET
Install the library
1. Download and unpack the archive.
2. Add a reference to Intelligencia.UrlRewriter.dll and place the file in your project’s bin directory.
3. Open your web.config file.
4. Add the following configuration:
<configSections> <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration. RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" /> </configSections>
<system.web> <httpModules> <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" /> </httpModules> </system.web>
You are now ready to rewrite URLs. Below are some examples.
Example: simple rewrite rule
Our application is a web shop that sells various types of media. In the current situation, we have a page that lists all products in a category as follows: example.com/products.aspx?category=dvd, and we want to shorten this to example.com/dvd.aspx.
Open the web.config file where you added UrlRewriter.NET and add the following rule:
<rewriter> <rewrite url="~/dvd.aspx" to="~/products.aspx?category=dvd" /> </rewriter>
Example: generalising with regular expressions
This works well if you have a small number of categories. But if you have many categories, it soon becomes unwieldy to add each new category manually. With the help of a regular expression, we can generalise the rule above.
<rewriter> <rewrite url="~/shop/(.+).aspx" to="~/products.aspx?category=$1" /> </rewriter>
This example rewrites all variants of example.com/shop/dvd.aspx to example.com/products.aspx?category=dvd.
Example: multiple capture groups
It would be useful to be able to view a summary of each individual product. This can be done by extending the rule above with another argument.
<rewriter> <rewrite url="~/shop/(.+)/(.+).aspx" to="~/products.aspx?category=$1&product=$2" /> </rewriter>
With this rule, visitors can request, for example, example.com/shop/dvd/terminator2.aspx, which rewrites to example.com/products.aspx?category=dvd&product=terminator2.
The pattern can be extended indefinitely if you categorise your products into deeper hierarchies.