in Development, Internet, Microsoft

IIS7 web.config fun

IIS7 LogoI spent some time last weekend migrating the Work Avoidance site away from Gradwell and onto a dedicated virtual server from MEMSET. Since this involved a move to a Windows box away from UNIX hosting, I had to configure in IIS7 some of the settings I had in the .htaccess under the UNIX host.

For example, I want a canonical host name (so visitors always see workavoidance.net with no www prefix in the address bar) and to ensure that the WordPress permalinks work properly. While I was at it I took the opportunity to disable hot-linking of our images, since for some reason a Russian website had taken a shine to some of our Armory graphics.

Below is the web.config I created. It redirects users to the canonical host name, enables WordPress permalinks and disables hot-linking of our images. The redirect proved something of a problem to begin with. Our iPhone App “Monkey Shaker” has an online high score system. Scores are submitted from the app via an encrypted HTTP POST to a PHP page which decrypts the information and updates the database. I discovered that when you issue a server side redirect most clients won’t re-POST their data to the new page – they will issue a GET for the new page and the original POST data is lost. This meant that highscore submissions were failing.

(NB: this isn’t as big a disaster as it sounds since we built a very robust mechanism to retry score submission into the app.)

So, you’ll note an extra entry in the web.config to exclude that page from a redirect:

<add input="{URL}" negate="true" pattern="scoresubmit\.php$" />

“Why not just change the URL in the app to be canonical?!” You may well ask. Yes, we probably will, but that relies on issuing an update and getting over a quarter of a million users to download it. This way we don’t have to rely on that.

Still to come – how I implemented caching in WordPress and in our high score system to improve performance.

Here’s the rewrite rules section of the completed web.config:

<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^workavoidance\.net$" />
<add input="{URL}" negate="true" pattern="scoresubmit\.php$" />
</conditions>
<action type="Redirect" url="http://workavoidance.net/{R:1}" redirectType="Permanent" />
</rule>
 
<rule name="Prevent image hotlinking">
<match url=".*\.(gif|jpg|png)$"/>
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" negate="true" />
<add input="{HTTP_REFERER}" pattern="^http://workavoidance\.net/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/no.png" />
</rule>
 
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>