exyus rewrite/redirect

2007-09-07 @ 23:55#

i spent the evening implementing a rewrite/redirect module for the Exyus framework. it's not as efficient as using a full ISAPI filter like ISAPI Rewrite, but it works pretty well for what i need. i wanted to be able to author some URL rules without having to edit the control file for the ISAPI filter. this is especially handy when you start adding up lots of custom rules for a single app.

i implemented two XML files: rewrite-urls.xml and redirect-urls.xml. the redirect file contains rules for sending 301 responses to clients. the rewrite file contains rules for internal path rewrites without bothering the client. you can also mix the two sets. for example, write a redirect rule to tag a trailing '/' at the end of a URL, then a rewrite rule that interprets that URL and converts it into the proper form for the engine to use.

i've also used .NET's named arguments when crafting my regular expressions. i like the fact the .NET allows two formats for names args: (?<arg-name>) and (?'arg-name'). since i am storing my rules in an XML file, having the second option is really handy.

here's an example of my rule to create a set of hackable URLs for poking around my blog archives:

<url
   path="/blogging/archives/(?'year'\d{4})/(?:(?'month'\d{1,2})?(?:/))?(?:(?'day'\d{1,2})?(?:/))?.xcs"
   newpath="/xcs/blogging/archives/.xcs?year=${year}&amp;month=${month}&amp;day=${day}" 
/>

code