As the title says I'm configuring Rewrite rules for Reverse Proxy functionality with:
- IIS 8.5
- Windows Server 2012R2
- URL Rewrite 2.1
- Application Request Routing 3.0
- Powershell 5.1
For this I'm using Powershell DSC, setting the configuration in a Script Resource. This works perfectly fine for the outbound rules, but the inbound rule does not get created, nor does it give an error/warning. When trying to set properties on it (on the 3 lines after), a warning does show saying it can't find the inbound rule, btw also showing the correctly used variable name). In IIS Management Console it is also not visible (and yes, I've used F5 on it).
I'm using the command as generated from IIS itself, which is equal to everything I see online, including StackOverflow. This particular problem is not easy to find, so I must be missing something very trivial. I've already tried:
- running the Script under different credentials
- using a hardcoded name instead of the variable
- using URL Rewrite 2.0
- restarting IIS
- rebooting server
The ONLY way I got the command working is if it's executed as a single command in (elevated) Powershell (marked below by >>>>).
Then the script itself. (for reference, one outbound rule is added too):
SetScript = {
Write-Verbose "Checking if inbound rewrite rule is present..."
$inbound = (Get-WebConfiguration -Filter "//System.webServer/rewrite/rules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:inboundRuleName"}
if($inbound -eq $null) {
Write-Verbose "Inbound rewrite rule not present, configuring..."
>>>> Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules" -Name "." -Value @{name=$using:inboundRuleName;stopProcessing="True"} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/match" -Name "url" -Value "^api/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "url" -Value "http://localhost:8000/api/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"
Write-Verbose "Inbound rewrite rule configured"
}
Write-Verbose "Checking if outbound HTML rule is present..."
$outboundHTML = (Get-WebConfiguration -Filter "//System.webServer/rewrite/outboundRules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:outboundHTMLRuleName"}
if($outboundHTML -eq $null) {
Write-Verbose "Outbound HTML rule not present, configuring..."
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions" -Name "." -Value @{name='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions/preCondition[@name='IsHTML']" -Name "." -Value @{input='{RESPONSE_CONTENT_TYPE}';pattern='^text/html'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules" -Name "." -Value @{name=$using:outboundHTMLRuleName;preCondition='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "filterByTags" -Value "A" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "pattern" -Value "^/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/conditions" -Name "." -Value @{input='{URL}';pattern='^/api/.*'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "value" -Value "/{C:1}/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"
Write-Verbose "Outbound HTML rewrite rule configured"
}
}
I hope someone knows why this is happening, as I'm getting pretty frustrated trying to solve this.