One of the most popular extensions to the Apache Web server has been mod_rewrite
- a filter which rewrites URLs. For example, instead of a URL such as
You could provide a filter which accepts URLs such as
And it will silently perform a
server-side redirect to the first URL. In this way, the real URL could be
hidden, providing an obfuscated facade to the web page. The benefits are easier
to remember URLs and increasing the difficulty of hacking a website.
‘mod_rewrite’ became very popular and grew to encompass a couple of
other features not related to URL Rewriting, such as caching. This blog
demonstrates URL Rewriting with ASP.NET, whereby the requested URL is matched
based on a regular expression and the URL mappings are stored in the standard ASP.NET web.config configuration file.
ASP.NET includes great caching facilities, so there's no need to duplicate mod_rewrite's caching functionality.
As more and more websites are
being rewritten with ASP.NET, the old sites which had been indexed by google
and linked from other sites are lost, of course culminating in the dreaded 404
error. I will show how legacy ASP sites can be upgraded to ASP.NET, while
maintaining links from search engines.
ASP.NET provides very limited
support out of the box. In fact, its support is down to a single method:
Which should be called during the
event in the Global.asax file. This is fine as
long as the number of URLs to rewrite is a small, finite, manageable number.
However, most ASP sites are in some way dynamic, passing parameters in the
Query String, so we require a much more configurable approach.
The storage location for all
ASP.NET Configuration information is the web.config
file, so we'd really like to specify the rewrites in there. Additionally, .Net
has a fast regular expression processor, giving free and fast search and
replace of URLs.