URL Rewriting for IIS7 and ColdBox
Posted by Jason Durham | Tags: ColdFusion , ColdBox , IIS
For a recent project, I was tasked with maintaining very clean and simple URLs. This required the use of an ISAPI filter. I started looking for free/open source projects I could use for this task and found two, Ionics ISAPI Rewrite Filter (IIRF) and the ever-popular Helicon ISAPI_Rewrite. Both of these filters are tried and tested ISAPI rewriters on IIS6 but weren't working as expected for me in IIS7. ColdBox provides some sample rules that work perfectly in Apache and IIS6, however they were causing my application to load twice on each request. I'm still not sure why and would appreciate if someone would relay an answer to that.
Here is what works on II6 (per ColdBox's SES Guide)...
IsapiRewrite.ini
IterationLimit 0
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(/.+/.+/.*\?.+\..*)$ /index.cfm/$1
RewriteRule ^(/[^.]*)$ /index.cfm/$1
These rules (along with the SES interceptor from ColdBox), should write the following URL...
http://www.mysite.com/foo/bar/
To the following URL for ColdBox/ColdCourse to use for routing...
http://www.mysite.com/index.cfm/foo/bar/
The application seemed to be behaving properly until I noticed a 'view counter' I have setup for my knowledgebase articles. I noticed that my view counter was incrementing by 2 on each request for an article. Weird. Here is the simple SQL statement...
UPDATE tbl_KnowledgeBase
SET KB_views = KB_views + 1
WHERE KB_ID =
My trusty ColdBox debugger was showing me that my knowledgebase article event was firing exactly once and my ColdFusion debugger was showing me that my addKBView() method was also firing once. This led me to the logs from the IIRF (and eventually) ISAPI_Rewrite filter. It seems those rules were the root of my mystery.
After two days of working with Luis and Sana Ullah, I finally stumbled upon a URL Rewrite Module built FOR IIS7 by MICROSOFT. Imagine that... I spent all of this time trying to find a 3rd party solution and never considered looking to Microsoft for the answer. :o)
After running a simple installer and, of course, rebooting, I found a new module available in the IIS7 MMC. Not only did it install flawlessly, but Microsoft even developed an import utility that accepts rules from an .htaccess file. I was floored. I went back to ColdBox's website, grabbed the .htaccess sample from the SES Guide and imported them in a matter of seconds. However, there is one caveat to this method... you will have to replace the server variable SCRIPT_NAME to PATH_INFO on two of the imported rules (should be self explanatory when you get to that point). Voila... my application fired up and my view counter worked properly!
Yesterday, someone from Helicon replied to a forum post of mine with a new set of rules for their rewriter which has also fixed my view counter issue. I'm still debating on whether to run two different URL rewriters (IIS7 is in development, IIS6 is in production) or to just use ISAPI_Rewrite for both machines. I have a feeling I'll switch to the latter. In any event, I've cleaned up some of the imported rules from the IIS7 import utility and provided them below. All you have to do is install the module, copy this code into a web.config file and throw it in your application's root (yes, you _could_ configure for all sites). If you have your Route.cfm and SES interceptor configured correctly, you should be ready to fire up your app sans "index.cfm" from the URL!
Download web.config sample file for ColdBox SES URLs without index.cfm
You'll notice there are also a few SQL injection protection rules in there provided by Luis Majano. Yet another little bonus that makes ColdBox such a powerful tool!
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment