IIS 7.0 Url Rewriter gotchas when importing mod_rewrite rules

One of the best things to happen to IIS 7.0 recently is the release of Url Rewriter, the IIS 7.0 url rewriting equivalent to Apache's mod_rewrite.

To ease the process of migrating Apache apps (notably PHP applications that rely on mod_rewrite rules for SEF / friendly urls), the Url Rewriter IIS Manager snapin includes an import function that can import mod_rewrite rules automatically.

Apparently not everyone knows about this little gem, so if you need to get mod_rewrite rules running on IIS 7.0, you should definitely use this to save yourself the few hours of learning the Url Rewriter configuration.  

But, in the first iteration, there are a couple gotchas to watch out for.  This got me pretty good the first few times I used the import, and I had to hunt around the FRT trace logs before I figured things out.

These should save you a couple hours:

1) The {REQUEST_URI} server variable

You may often end up with this variable in imported rules.

Unfortunately, the current release of Url Rewriter has an issue that causes this variable to expand to "" (empty) when processing requests to static files.  This means your rules for jpeg, html, etc extensions will likely fail to run, causing funky results.

Workaround: Replace the {REQUEST_URI} with {URL}  in web.config (see example below).

2) Case-sensitivity

By default, IIS Manager import generates case-sensitive rules.  There is no option to perform a case-insensitive import, although you can edit each rule manually afterwards.

Most urls on IIS 7.0 are are case-insensitive.  This means that much of the time, requests that use case variations may fail to match any of your imported rules, and cause funky behavior.

Workaround: Edit each rule to make it case-insensitive.  You can do it in IIS Manager or in the web.config directly, i.e.:

<rewrite>

  <rules>

    <rule name="Imported Rule 1" stopProcessing="true">

      <match url="^(.*)$" ignoreCase="true" />

      <conditions logicalGrouping="MatchAll">

        <add input="{URL}" negate="true" pattern="^/myapp/stylesheets/" ignoreCase="true" />

        …

      </conditions>

      <action type="Rewrite" url="/myapp/index.php?title={R:1}" appendQueryString="true" />

    </rule>

  </rules>

</rewrite>

 

For a list of other known issues, be sure to review: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/ (scroll down to "Known Issues").

This should help you avoid some frustration and get your imported rules working right away.

In the meantime, the Url Rewriter team is aware of these issues and is looking into addressing them.  Thankfully with the out of band delivery model afforded by the IIS 7.0 modular architecture, issues like these can be addressed fairly quickly 🙂

Thanks,

Mike

14 Comments

  1. Anonymous

    Hello!
    Is there a way to automate the import process? My probleme is: I used apache before and some clients used htaccess files.. I want to automate / delegate the import function.

    Thanks for you answer!
    Peter

  2. Mike Volodarsky

    Hi Peti,

    There is no supported way to automate the import process, but you could probably use reflection to do the import somewhat automatically.

    Send me a mail with more details of what you are trying to do, and I may be able to give you some further advice …

    Thanks,

    Mike

  3. Anonymous

    Very very very useful!

    It’s a shame there’s no automation, but it’s a small gripe for an otherwise great addition to Windows Server 🙂

  4. thstart

    I have a simple question. How to redirect http requests to one virtual directory and https to other?

    For example http:www.a.com to go to c:a and https:www.a.com to go to c:b?

  5. Anonymous

    I’ve been trying to make the URL Rewriter work with the Magento ecommerce software. The system is working to some extent insofar as I can get the rewritten URLs but the system always takes me back to the home page instead of to the correct page. These are the rules I’ve imported:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/(?!media|skin|js|var).* index.php [L]

    Any thoughts would be appreciated.

  6. Anonymous

    I am also sitting here and trying to get magento up and running with IIS. Everything ist working fine already, but I have lots problems to translate the rewrite rules from magento to the IIS modrewrite.

    I would like to share thoughts to sort out this final last piece of the puzzle :O)

  7. Mike Volodarsky

    Hi Andreas,

    I am sure the IIS team will welcome your feedback on the Url Rewriter – the most direct way is to post on the appropriate forum on forum.iis.net.

    Best,
    Mike

Leave a Reply

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