powershellhttpsurl-rewrite-module

Viewing IIS URL Rewrite Rules in Powershell


So I have a web based application that I'm responsible for installing at our customers. This requires a lot of manual configuration within IIS, and I've been able to do almost all of this via Powershell. But there's one key thing I'm struggling with and that's the URL Rewrite settings I need to apply. The first one I figured out, and that's to add a rule forwarding the default website to a specific site. I managed this with:

$filterRoot = "system.webServer/rewrite/rules/rule[@name='RootRedirect$_']"
Clear-WebConfiguration -pspath $site -filter $filterRoot
Add-WebConfigurationProperty -pspath $site -filter "system.webServer/rewrite/rules" -name "." -value @{name='RootRedirect' + $_ ;patternSyntax='Regular Expressions';stopProcessing='True'}
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/match" -name "url" -value "(^$)"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/conditions" -name "logicalGrouping" -value "MatchAny"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "type" -value "Redirect"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "url" -value "/redirectedlink"

The second one I'm trying to figure out is how to redirect any incoming connections to https. I know the settings that have to go in, so my initial thought is to create it manually, then use Get-WebConfigurationProperty to see what the settings look like in PowerShell so I have some idea how to recreate them. But I can't seem to figure out how to get them. I've got the rule currently configured with the name HTTPSRedirectTest, but I can't figure out how to see the rule in PowerShell. I've found all sorts of things about Get-WebConfigurationProperty, but can't find anything in relation to the URL Rewrite settings. Anyone have any tips on this?


Solution

  • I found this and it worked for me!

    https://dbaland.wordpress.com/2019/02/13/create-a-http-to-https-url-redirect-in-iis-with-powershell/

    Edit: In the end I came up with this I was able to figure out how to view the settings, but couldn't figure out exactly what I was looking at. I ended up using the link above to come up with this:

    #Declare variable for path to default website
    
        $site = "iis:\sites\Default Web Site"
    
    #Create HTTPS Redirect
    
        $FilterHTTPSredirect = "system.webServer/rewrite/rules/rule[@name='HTTPSredirect$_']"
        Clear-WebConfiguration -pspath $site -filter $FilterHTTPSredirect
        Add-WebConfigurationProperty -PSPath $site -filter "system.webServer/rewrite/rules" -name '.' -value @{name='HTTPSredirect' + $_ ; patterSyntax='Regular Expressions'; stopProcessing='True'}
        Set-WebConfigurationProperty -PSPath $site -filter "$FilterHTTPSredirect/match" -name 'url' -value "(.*)"
        Set-WebConfigurationProperty -PSPath $site -filter "$FilterHTTPSredirect/conditions" -name '.' -value @{input='{HTTPS}'; matchType='0'; pattern='^OFF$'; ignoreCase='True'; negate='False'}
        Set-WebConfigurationProperty -PSPath $site -filter "$FilterHTTPSredirect/action" -name 'type' -value 'Redirect'
        Set-WebConfigurationProperty -PSPath $site -filter "$FilterHTTPSredirect/action" -name 'url' -value 'https://{HTTP_HOST}{REQUEST_URI}'
    
    #Disable HTTPS Redirect Rule
    
        set-webconfigurationproperty '/system.webserver/rewrite/rules/rule[@name="HTTPSredirect"]' -Name enabled -Value false -PSPath "IIS:\sites\Default Web Site"
    

    It adds the https redirect rule, but also disables it as not every site wants to use it, and there are additional configurations required elseswhere if they do wish to have it enabled.