Part 2: Masking hgwebdir.cgi in our URL
This blog post is part of a four part series for getting Mercurial up and running in a Windows environment and hosting it on IIS 5.1 (i.e. Windows Server 2003).
- Part 1 Getting Mercurial set up in IIS and hosting a repository at http://hg.mydomain.com/
- Part 2 Getting IIS to hide hgwebdir.cgi in the URL for your Mercurial repositories.
- Part 3 Locking down security using Active Directory so that only authorized users can view, clone, pull and/or push to a repository.
- Part 4 Customizing the theme/skin of the web interface of your Mercurial repositories.
In part 1 we got Mercurial up and running in IIS and created a repository that we can pull from and push to.
The next step is to get rid of that god awful hgwebdir.cgi in the URL so we can have a nice URL - perhaps like: http://hg.mydomain.com/Repos/Project1 instead of http://hg.mydomain.com/hgwebdir.cgi/Project1...
- Download and install Helicon's ISAPI Rewrite 3 - Note: if you need ISAPI Rewrite to work only on specific sites on your server, you need the $99 version. If it can be applied to all sites hosted on your server you can get the free lite version.
- Open the ISAPI Rewrite manager.
- Click the website (if you purchased the $99 version) or the IIS Web Sites node if you got ISAPI Rewrite Lite.
- On the right side of the window you will see the config file listed as <InstallDir>\Helicon\ISAPI_Rewrite3\httpd.conf. Click the edit button.
- Add the following rule:
- RewriteRule /[<ReposFolder>/](.*) /hgwebdir.cgi/$1 [I,O]
- Set the baseurl property in your hgweb.config file to the nice display name
you set in the previous step.
[web] baseurl = /Repos/
The reason we replace hgwebdir.cgi with a nice name instead of just hiding it altogether is that we only want the rule to fire when we're calling repositories, not when we're calling other resources on the server. If for instance we were to use:
RewriteRule /(.*) /hgwebdir.cgi/$1 [I,O]
This would fire for every page we requested regardless of whether or not we were looking for a repository or not. The idea is that you only want the rule executed in a minimal set of circumstances.
Now we can access our repository using http://hg.mydomain.com/Repos/Project1 and the cgi will be invoked correctly because the rule rewrote the address from http://hg.mydomain.com/Repos/Project1 to http://hg.mydomain.com/hgwebdir.cgi/Project1.