微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Powershell 中查看 IIS URL 重写规则

如何解决在 Powershell 中查看 IIS URL 重写规则

所以我有一个基于 Web 的应用程序,我负责在我们的客户处安装它。这需要在 IIS 中进行大量手动配置,而我几乎可以通过 Powershell 完成所有这些操作。但是我遇到了一件关键的事情,那就是我需要应用的 URL 重写设置。我想出了第一个,那就是添加一个规则,将认网站转发到特定网站。我通过以下方式管理:

$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"

我想弄清楚的第二个是如何将任何传入连接重定向到 https。我知道必须输入的设置,所以我最初的想法是手动创建它,然后使用 Get-WebConfigurationProperty 查看 PowerShell 中的设置是什么样的,这样我就知道如何重新创建它们。但我似乎无法弄清楚如何获得它们。我已将规则当前配置为名称 HTTPSRedirectTest,但我不知道如何在 PowerShell 中查看该规则。我找到了有关 Get-WebConfigurationProperty 的各种信息,但找不到与 URL 重写设置相关的任何内容。有人对此有什么建议吗?

解决方法

我发现了这个,它对我有用!

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

编辑:最后我想出了这个我能够弄清楚如何查看设置,但无法弄清楚我在看什么。我最终使用上面的链接想出了这个:

#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"

它添加了 https 重定向规则,但也将其禁用,因为并非每个站点都想使用它,并且如果他们确实希望启用它,则需要在其他地方进行其他配置。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。