Installing an MVC Application in a subdirectory of WordPress

540px-WordPress_logo.svgFor several reasons I have WordPress installed in the root directory of my hosted web space. I’ve been experimenting with the new ASP.NET MVC framework, Entity Framework 4, SQL Server Compact Edition 4 and other new technologies, and I wanted to do a test deployment. I was quite surprised when, after deploying to a subdirectory of my web site I could not bring up the page; WordPress was attempting to handle the request even though the path was clearly to the subdirectory.

The culprit: WordPress’s  pretty permalinks feature. I’ve ranted in the past about how there is a fetish for URLs, and how so much effort, for better or for worse, is given toward making them more human readable. In the case of a WordPress install on an IIS7 Windows server, the permalink feature is achieved using the Url Rewrite Module, version 2.

The module is configured using the web.config file, and the configuration block for the Url Rewrite engine looks like this:

<system.webServer>
  <rewrite>
    <rules>
      <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>
    </rules>
  </rewrite>
</system.webServer>

The rewrite rule tells IIS7 to intercept all requests of a certain form and route them to index.php. This is not what I want, I would like requests that ask for a directory that actually exists to go to that directory. In fact, I don’t know how to do this, nor do I have the time to learn the complex syntax of the Url Rewrite engine. However, using the examples given in several places on the web I figured out how to tell the Rewrite engine to direct certain requests to the proper directories.

The solution: add the following rule before the WordPress rule (shown above)

<system.webServer>
  <rewrite>
    <rules>
      <rule name="new_directory" stopProcessing="true">
        <match url="new_directory" />
      </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>
    </rules>
  </rewrite>
</system.webServer>

This rule tells the rewrite engine to take requests for “new_directory” and ignore them completely — it stops processing the rules once this rule is matched, and allows the request to be handled by the code in the subdirectory. There were a few other tweaks that I had to do that were specific to my web host; it you’re interested, comment below and I’ll provide the details.

Posted in Blog and tagged , , , , , .

2 Comments

  1. thanks!! i have spent the better part of my friday looking for this, and it was frustrating the living flip out of me.

    this worked perfectly.

  2. Can you imagine something you wrote 3 years ago helped someone today!? thanks so much for this post. It fixed a problem that’s been plaguing me!

    As my daughters Kindergarten teacher would say, “You get a gold star today!”

Leave a Reply

Your email address will not be published. Required fields are marked *