RICHARD FAWCETT'S WEBSITE

ABOUT     CONTACT     RSS


5 September 2012
Using URL Rewrite in IIS7 to have all Requests Handled by a Script

In my post last year about setting up Mercurial on IIS, I left it as an exercise for the reader to allow it so that index.py could be left out of the URLs. As an example, we want to be able to write http://myserver/myrepo instead of http://myserver/index.py/myrepo.

Enter URL Rewrite!

The easiest way to get URL Rewrite installed on your web server is to use Microsoft's Web Platform Installer. Install and run it on your server, and choose to install URL Rewrite 2.0. (While you're at it, I'd also recommend installing IIS Recommended Configuration too).

Now it's installed, you will get a "URL Rewrite" icon in IIS Manager when you select your website. Open it and choose to add a new, blank rule.

Configuring the rule is easy, and we'll use a RegEx trick known as a negative look-ahead assertion. In the match URL section, enter the pattern: ^(?!index.py)(.+). In the Rewrite URL box, just enter: index.py/{R:1}. Leave all the other options as they are, give your rule a name, and hit apply (top right hand corner).

Creating a rule with URL Rewrite

That's all there is to it. The match is setup to match any URL which doesn't start with index.py, so now, any request that doesn't start with index.py will be rewritten so that index.py is inserted at the start of the path.