Iis – Url Rewrite Rule needs to disabled and enabled to IIS

iispowershellrewrite

I am automating the creation of a URL Rewrite in a testing environment. I am configuring it using Powershell in IIS 8.5

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Default Web Site/App/MyApp/v1' -filter "system.webServer/rewrite/allowedServerVariables" -name "." -value @{name='HTTP_X_FORWARDED_FOR'}
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Default Web Site/App/MyApp/v1' -filter "system.webServer/rewrite/rules" -name "." -value @{name='Add X-Forwarded-For Header';patternSyntax='Wildcard'}
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Default Web Site/App/MyApp/v1' -filter "system.webServer/rewrite/rules/rule[@name='Add X-Forwarded-For Header']/match" -name "url" -value "*"
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Default Web Site/App/MyApp/v1' -filter "system.webServer/rewrite/rules/rule[@name='Add X-Forwarded-For Header']/serverVariables" -name "." -value @{name='HTTP_X_FORWARDED_FOR';value='{REMOTE_ADDR}';replace='False'}

This creates the rule however the IIS Application is broken when trying to make a request.

It throws an 500.52 error.

enter image description here

However, if the rule is disabled and enabled in the UI everything is fine. What is going on?

Best Answer

Your PowerShell code has in typo in the last line HTTP_X_ORWARDED_FOR has to be HTTP_X_FORWARDED_FOR

On IIS 10 this results in the error:

HTTP Error 500.50 - The server variable "HTTP_X_ORWARDED_FOR" is not allowed to be set. Add the server variable name to the allowed server variable list.

With PowerShell you are updating the global applicationhost.config, when using the GUI, the changes are written into the web.config file. I have no idea why that fixes your problem. I still get the same error as before.

Fix the typo and it should work.