RewriteCond Directives Evaluated After RewriteRule Directives In URL Rewriting

Posted September 4, 2009 at 11:19 AM by Ben Nadel

Tags: ColdFusion, Search Engine Optimization

This is a minor point, but an important one regarding the control flow of directives within IIS Mod-Rewrite's URL rewriting configuration files. When I first looked at the RewriteCond (rewrite condition) and RewriteRule (rewrite rule) directives, I thought of them like programmatic IF-Statements such that the RewriteCond directives would be evaluated before the RewriteRule. So, for example, if we have the statement collection:

  • RewriteCond %{REQUEST_FILENAME} !-f
  • RewriteRule ^(.+)$ index.cfm

... I thought this was being evaluated as (pseudo code):

  • if (REQUEST_FILENAME does not exist){
  • if (REQUEST.Matches( "^(.+)$" )){
  • rewrite( "index.cfm" );
  • }
  • }

According to the IIS Mod-Rewrite documentation, however, my assumption is actually way off. In fact, the order of evaluation of directives is quite the opposite; RewriteCond directives are not evaluated before RewriteRule, but rather after the RewriteRule as secondary conditional assertions. As the configuration file is executed, the RewriteRule regular expression is applied to the request; if the RewriteRule regular expression matches, then and only then are the preceding RewriteCond directives evaluated; and, if all the RewriteCond directives are true, the RewriteRule directive is applied to the request.

So, taking the same example above, the actual control flow is more like this:

  • if (REQUEST.Matches( "^(.+)$" )){
  • if (REQUEST_FILENAME does not exist){
  • rewrite( "index.cfm" );
  • }
  • }

While the outcome would be the same for us in either of the pseudo code examples above, not understanding the order of operations could lead to redundancy and extra processing. Take this collection of directives for example:

  • RewriteCond %{REQUEST_URI} /$
  • RewriteRule ^(.+)$ index.cfm

In the above collection, the RewriteCond directive requires the requested URL to end in "/". Now, if we operated under the assumption that the RewriteCond directives were processed first, then we would assume that only the RewriteCond directive would execute, evaluate to false, and the RewriteRule would be passed-over. However, what actually happens is that the RewriteRule is evaluated, matches the request, then evaluates the RewriteCond directive to see if the rewrite should take place. Now, we have two regular expressions being applied rather than just one (had we put the slash-constraint in our RewriteRule pattern).

This might seem like a contrived example, but, by not fully understanding the control flow of the IIS Mod-Rewrite rules engine, I could end up with a bunch of processing that I don't need. I am sure that for many of you familiar with the URL rewriting world, this is a no-brainer; but, as someone who creates a thousand compound IF-Statements every day, this control flow was not the expected behavior.




Reader Comments

Sep 9, 2009 at 1:39 PM // reply »
1 Comments

thanks man


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools