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

Published 07 December 08 09:16 by Mike Volodarsky
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MVolo's Blog said on December 7, 2008 10:31 PM:

One of the best things to happen to IIS 7.0 recently is the release of Url Rewriter , the IIS 7.0 url

# IIS 7 Rewrite said on December 11, 2008 1:17 PM:
The URL Rewrite extension is a awesome; finally an IIS translation for mod_rewrite style rules!
# peti said on December 14, 2008 6:06 AM:
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
# Mike Volodarsky said on December 14, 2008 7:00 PM:

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

# Abeon said on January 17, 2009 2:06 PM:
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 :)
# thstart said on January 26, 2009 11:39 AM:

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?

# Jim said on February 25, 2009 8:38 PM:
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.
# Andreas said on March 16, 2009 8:11 AM:

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)

# Mike Volodarsky said on March 18, 2009 12:44 AM:

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

# Ruslan Yakushev said on April 21, 2009 12:54 AM:
The URL Rewrite 1.1 has a fix for the bug in the rule import tool related to {REQUEST_URI}. More information about URL Rewrite 1.1 is here: http://ruslany.net/2009/04/url-rewrite-module-11-for-iis-7/
# Mike Volodarsky said on April 23, 2009 2:24 PM:

Ruslan, thanks for the update! This is great to hear.

Thanks,

Mike

# NewsPeeps said on August 8, 2009 1:21 PM:

Thank you for submitting this cool story - Trackback from NewsPeeps

# Winnie said on January 31, 2010 12:34 PM:
Has anyone found a solution for the url rewrite on magento yet??

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below


About Mike Volodarsky

For the past 5 years, I was the core Program Manager for Microsoft ASP.NET 2.0 and IIS 7.0 products. I drove the design and development of the IIS 7.0 web server core, the IIS FastCGI support, the AppCmd command line tool, the ASP.NET Integrated pipeline, and other special projects around server security, performance, and scalability. Now, I am working on my own on cutting edge web server tech on top of the Microsoft IIS platform, and continue blogging about it here.

About me



For the past 5 years, I was the core server Program Manager for the IIS 7.0 and ASP.NET 2.0 products at Microsoft.
Now, I work on advanced web server tech using IIS 7.0, .NET, and Windows Server 2008 and write about it in this blog.

View Michael Volodarsky's profile on LinkedIn

Writings



TechNet Magazine
>Top 10 Performance Improvements in IIS 7.0

MSDN Magazine
>IIS 7.0: Build Web Server Solutions with End-To-End Extensibility
>IIS 7.0: Enhance Your Apps with the Integrated ASP.NET Pipeline
>IIS 7.0: Explore The Web Server For Windows Vista And Beyond
>Design and Deploy Secure Web Apps with ASP.NET 2.0 and IIS 6.0
>Fast, Scalable, and Secure Session State Management for Your Web Applications


Tools and Modules

LeechGuard
IconHandler 2.0
DirectoryListing
HttpRedirection
IIS Auth for Wordpress
iisschema.exe
PortCheck.exe v2.0

Popular Posts

- ASP.NET 2.0 Breaking Changes on IIS 7.0
- Develop IIS7 modules and handlers with .NET
- Troubleshoot IIS7 errors like a pro
- Troubleshooting 503 / "service unavailable" errors
- Troubleshooting "server not found" errors
- Create IIS7 sites, applications, and virtual directories
- Run Ruby on Rails with IIS FastCGI
- VS Debugging of ASP.NET applications on Windows Vista
- Stop hot-linking with IIS and ASP.NET

Tags

Search

Go

This Blog

Archives

Good IIS Blogs

Disclaimer

These postings are provided as is with no warranties, and confer no rights. The views expressed in this blog are entirely my own.

Syndication