How to Export IIS7 URL Rewrite Rules for Seamless Migration and Testing


1 views

In IIS7+, URL rewrite rules are stored in either:

  • %SystemRoot%\system32\inetsrv\config\applicationHost.config (server-level)
  • Your site's web.config file (site-specific)

For site-specific rules, simply copy the web.config file. For server-level rules:




  
    
      
    
    
      
    
  

Create an export script (Export-RewriteRules.ps1):


# Export all rewrite rules to XML
Import-Module WebAdministration
$rules = Get-WebConfigurationProperty -Filter "/system.webServer/rewrite/rules" -Name collection
$rules | Export-Clixml -Path "C:\rewrite_rules_export.xml"

# For specific site:
$siteRules = Get-WebConfigurationProperty -PSPath "IIS:\Sites\YourSiteName" -Filter "/system.webServer/rewrite/rules" -Name collection
$siteRules | Export-Clixml -Path "C:\site_rewrite_rules.xml"

Command-line alternative:


:: Export all rewrite rules
appcmd list config -section:system.webServer/rewrite/rules /xml > C:\rewrite_rules.xml

:: Export for specific site
appcmd list config "Default Web Site/" -section:system.webServer/rewrite/rules /xml > C:\site_rules.xml

When importing to new environment:

  1. Validate rule conditions match new environment paths
  2. Check for hardcoded domains in rules
  3. Test with failedRequest tracing enabled

If rules don't work after import:

  • Verify rewriteModule is installed on target server
  • Check IIS feature delegation settings
  • Confirm XML syntax validity (especially escaped characters)

For developers managing IIS7 environments, rewrite rules are stored in the application's web.config file under the system.webServer/rewrite/rules section. Here's a typical structure:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

While there's no direct "Export" button, you can:

  1. Open IIS Manager and select the website
  2. Double-click "URL Rewrite" module
  3. Right-click any rule and choose "View Server Variables" (shows all rules)
  4. Copy the entire <rewrite> section from web.config

For large-scale deployments, use this PowerShell script to extract rules:

# Load Web.config
$webConfigPath = "C:\inetpub\wwwroot\your-site\web.config"
$xml = [xml](Get-Content $webConfigPath)

# Extract rewrite rules
$rewriteRules = $xml.SelectNodes("//system.webServer/rewrite/rules/rule")

# Export to new XML
$exportXml = New-Object System.Xml.XmlDocument
$root = $exportXml.CreateElement("ExportedRewriteRules")
$exportXml.AppendChild($root)

foreach ($rule in $rewriteRules) {
    $importedNode = $exportXml.ImportNode($rule, $true)
    $root.AppendChild($importedNode)
}

$exportXml.Save("C:\temp\rewrite-rules-export.xml")

Once exported, you can insert the rules into another web.config using:

$targetConfig = [xml](Get-Content "C:\target-site\web.config")
$sourceRules = [xml](Get-Content "C:\temp\rewrite-rules-export.xml")

$rewriteNode = $targetConfig.CreateElement("rewrite")
$rulesNode = $targetConfig.CreateElement("rules")

foreach ($rule in $sourceRules.ExportedRewriteRules.ChildNodes) {
    $importedNode = $targetConfig.ImportNode($rule, $true)
    $rulesNode.AppendChild($importedNode)
}

$rewriteNode.AppendChild($rulesNode)
$targetConfig.configuration["system.webServer"].AppendChild($rewriteNode)
$targetConfig.Save("C:\target-site\web.config")

For teams using Git, consider adding a pre-commit hook to automatically export rules:

#!/bin/sh
# .git/hooks/pre-commit
powershell -File "scripts/export-rewrite-rules.ps1"
git add web.config