Index: wp-admin/includes/misc.php
===================================================================
--- wp-admin/includes/misc.php	(revision 11919)
+++ wp-admin/includes/misc.php	(working copy)
@@ -154,11 +154,11 @@
 	// Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
 	if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) {
 		if ( iis7_supports_permalinks() ) {
-			$rule = $wp_rewrite->iis7_url_rewrite_rules();
-			if ( ! empty($rule) ) {
-				return iis7_add_rewrite_rule($web_config_file, $rule);
+			$rules = $wp_rewrite->iis7_url_rewrite_rules();
+			if ( ! empty($rules) ) {
+				return iis7_add_rewrite_rules($web_config_file, $rules);
 			} else {
-				return iis7_delete_rewrite_rule($web_config_file);
+				return iis7_delete_rewrite_rules($web_config_file);
 			}
 		}
 	}
@@ -457,39 +457,14 @@
 }
 
 /**
- * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
+ * Delete WordPress rewrite rules from web.config file if it exists there
  *
  * @since 2.8.0
  *
- * @return bool
- * @param string $filename The file path to the configuration file
- */
-function iis7_rewrite_rule_exists($filename) {
-	if ( ! file_exists($filename) )
-		return false;
-	if ( ! class_exists('DOMDocument') )
-		return false;
-
-	$doc = new DOMDocument();
-	if ( $doc->load($filename) === false )
-		return false;
-	$xpath = new DOMXPath($doc);
-	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
-	if ( $rules->length == 0 )
-		return false;
-	else
-		return true;
-}
-
-/**
- * Delete WordPress rewrite rule from web.config file if it exists there
- *
- * @since 2.8.0
- *
  * @param string $filename Name of the configuration file
  * @return bool
  */
-function iis7_delete_rewrite_rule($filename) {
+function iis7_delete_rewrite_rules( $filename ) {
 	// If configuration file does not exist then rules also do not exist so there is nothing to delete
 	if ( ! file_exists($filename) )
 		return true;
@@ -503,11 +478,12 @@
 	if ( $doc -> load($filename) === false )
 		return false;
 	$xpath = new DOMXPath($doc);
-	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
+	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[contains(@name,\'wordpress\')]');
 	if ( $rules->length > 0 ) {
-		$child = $rules->item(0);
-		$parent = $child->parentNode;
-		$parent->removeChild($child);
+		foreach ( $rules as $child ) {
+			$parent = $child->parentNode;
+			$parent->removeChild($child);		
+		}
 		$doc->formatOutput = true;
 		saveDomDocument($doc, $filename);
 	}
@@ -515,15 +491,15 @@
 }
 
 /**
- * Add WordPress rewrite rule to the IIS 7 configuration file.
+ * Add WordPress rewrite rules to the IIS 7 configuration file.
  *
  * @since 2.8.0
  *
  * @param string $filename The file path to the configuration file
- * @param string $rewrite_rule The XML fragment with URL Rewrite rule
+ * @param string $rewrite_rules The XML fragment with URL Rewrite rule
  * @return bool
  */
-function iis7_add_rewrite_rule($filename, $rewrite_rule) {
+function iis7_add_rewrite_rules($filename, $rewrite_rules) {
 	if ( ! class_exists('DOMDocument') )
 		return false;
 
@@ -542,10 +518,14 @@
 
 	$xpath = new DOMXPath($doc);
 
-	// First check if the rule already exists as in that case there is no need to re-add it
-	$wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
-	if ( $wordpress_rules->length > 0 )
-		return true;
+	// First check if the rules already exist as in that case we need to remove them before writing the updated ones.
+	$wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[contains(@name,\'wordpress\')]');
+	if ( $wordpress_rules->length > 0 ) {
+		foreach ( $wordpress_rules as $child ) {
+			$parent = $child->parentNode;
+			$parent->removeChild($child);		
+		}
+	}
 
 	// Check the XPath to the rewrite rule and create XML nodes if they do not exist
 	$xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
@@ -584,7 +564,7 @@
 	}
 
 	$rule_fragment = $doc->createDocumentFragment();
-	$rule_fragment->appendXML($rewrite_rule);
+	$rule_fragment->appendXML($rewrite_rules);
 	$rules_node->appendChild($rule_fragment);
 
 	$doc->formatOutput = true;
Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 11919)
+++ wp-includes/rewrite.php	(working copy)
@@ -1720,15 +1720,29 @@
 		if ( ! $this->using_permalinks()) {
 			return '';
 		}
-		$rules  = "<rule name=\"wordpress\" patternSyntax=\"Wildcard\">\n";
-		$rules .= "	<match url=\"*\" />\n";
-		$rules .= "	<conditions>\n";
-		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />\n";
-		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />\n";
-		$rules .= "	</conditions>\n";
-		$rules .= "	<action type=\"Rewrite\" url=\"index.php\" />\n";
-		$rules .= "</rule>";
 
+		$rules = "\n";
+		$counter = 0;
+		//add in the rules that don't rewrite to WP's index.php (and thus shouldn't be handled by WP at all)
+		foreach ( (array) $this->non_wp_rules as $match => $query ) {
+			$rules .= '<rule name="non_wordpress_rule_' . $counter++ . '" stopProcessing="true">' . "\n";
+			$rules .= ' <match url="' . $match . '" />' . "\n";
+			// Apache uses $N (N=0,..,9) for back-references. IIS URL rewrite uses {R:N} (N=0,...,9)
+			// We need to replaces all the back-referenses
+			$query = preg_replace('/\$(\d)/i', '{R:$1}', $query);
+			$rules .= ' <action type="Rewrite" url="' . $query . '" />' . "\n";
+			$rules .= '</rule>' . "\n";
+		}
+
+		$rules .= '<rule name="wordpress" patternSyntax="Wildcard">'."\n";
+		$rules .= ' <match url="*" />'."\n";
+		$rules .= ' <conditions>'."\n";
+		$rules .= '  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />'."\n";
+		$rules .= '  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />'."\n";
+		$rules .= ' </conditions>'."\n";
+		$rules .= ' <action type="Rewrite" url="index.php" />'."\n";
+		$rules .= '</rule>';
+
 		$rules = apply_filters('iis7_url_rewrite_rules', $rules);
 
 		return $rules;
