Converting mod_rewrite rules to IIS7 url rewrite rules

The way you can extend IIS 7 today is awesome.  One feature that I particularly enjoy is the IIS7 URL rewrite rule engine. Below I’ll cover the IIS7 URL rewrite rule engine and how to convert preexisting mod_rewrite rules.

Installation

You can install the IIS 7 extension by using the Microsoft Web Platform installer

Reusing previous mod_rewrite rules

In IIS the entire configuration is XML driven.  Apache mod_rewrite is the incumbent technology that the IIS7 team piggybacked, by tapping into their preexisting DSL – Domain Specific Language.  They wrote a converter that takes mod_rewrite rules as the input, and transforms it into a IIS7 XML configuration.  This strategy allows previous rules to be converted quickly to IIS7 configuration.

This is what the user interface for this tranform will look like:

I always write my rules using mod_rewrite terse syntax, and use the IIS7 converter to create the verbose XML equivalent.  The rules ensure a really good DSL design, which has withstood the test of time for at least 10 years, when I was first introduced to it. Conditions have been applied to the first rewrite rule, as your read the document from top to bottom. You can then use back referencing to RewriteCond, using ‘%’, or back referencing to the matched path of the RewriteRule, using ‘$’ .  The flags at the end of the RewriteRule allow you to use the following codes.  R = redirect status code, QSA = Append Query String, and L = tells the interpreter to stop processing if this rule matches. There are also skip flags that allow you to apply conditional logic with the interpreter.  For more info on the flags read: http://httpd.apache.org/docs/current/rewrite/flags.html  or http://httpd.apache.org/docs/current/mod/mod_rewrite.html Here are some of the rules that showcase some common uses of the IIS rewrite rules engine.

          #redirect https://domain.com to https://www.domain.com ; following two rules can probably be merged by backreferencing REQUEST_SCHEME
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ https://www.%1.%2/$1 [R=301,L]

#redirect http://domain.com to http://www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]

#redirect files referenced using /Data to reroute to /Tenants/www.domain.com/Data/ back referencing url path after data and append the query string
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$
RewriteRule ^/Data(.*)$ /Tenants/%1.%2.%3/Data/$1 [QSA]

#redirect if http force and redirect to http
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%1.%2.%3/$1 [R=301,QSA,L]

        

Debugging

If you need to debug logic locally by using rewrite rules, then you will need to have vs2010 sp1, and you will also have to enable IIS 7 Express to run the project.  This will allow the Webserver element in your web.config to run locally without using the full version of IIS 7.