【问题标题】:How to redirect iisstart.htm via command line?如何通过命令行重定向 iisstart.htm?
【发布时间】:2019-04-04 10:13:38
【问题描述】:

我在 Windows Server 2016 上运行 iis10。 我需要将 iisstart.htm(默认 iis 登录页面)重定向到另一个 url。 有没有办法用命令行来做到这一点? 最好是powershell/appcmd。

我发现最接近的是这个命令,但这不是我需要的,因为它将任何请求重定向到这个 url,并且只有在 url 中没有请求特定的 Web 应用程序时我才需要重定向。

Set-WebConfiguration system.webServer/httpRedirect "IIS:\sites\Default Web Site" -Value @{enabled="true";destination="domain.com";exactDestination="true";httpResponseStatus="Permanent"}

【问题讨论】:

    标签: windows powershell iis cmd url-redirection


    【解决方案1】:

    IISStart.htm 是默认登录页面的唯一原因是因为它被配置为默认文档。如果您只想重定向到不同的文件,请使用默认文档。在这里阅读它 (https://docs.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/) 以及一些脚本:

    清除默认文档中的所有条目

    Remove-WebConfigurationProperty  -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/defaultDocument/files" -name "."
    

    添加新的默认文档

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/defaultDocument/files" -name "." -value @{value='MySpecialLandingPage.html'}
    

    从外观上看,您希望重定向到像 OWA 这样的文件夹。使用 HTTPRedirect(使用文件特定配置)在技术上可能是可行的,但即使可能,我也强烈不建议使用该功能。相反,我建议使用 URL Rewrite,不幸的是,它可以在此处单独下载:https://www.iis.net/downloads/microsoft/url-rewrite 在我看来,URL Rewrite 应该在所有 IIS 服务器上都可用。

    可以在此处找到一些关于 URL 重写的好示例:https://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

    否则,你想要的具体规则我相信如下: 1) 仅匹配未指定文件或文件夹的 URL (http://hostname/)。 2) 将这些请求重定向到http://hostname/owa/

    示例 URL 重写脚本

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules" -name "." -value @{name='RedirectDefaultDocument';stopProcessing='True'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules/rule[@name='RedirectDefaultDocument']/match" -name "url" -value "^$"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules/rule[@name='RedirectDefaultDocument']/action" -name "url" -value "owa/"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules/rule[@name='RedirectDefaultDocument']/action" -name "appendQueryString" -value "False"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules/rule[@name='RedirectDefaultDocument']/action" -name "redirectType" -value "Temporary"
    
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/rewrite/rules/rule[@name='RedirectDefaultDocument']/action" -name "type" -value "Redirect"
    
    

    【讨论】:

    • 有趣的解决方案。我将对此进行调查并更新我们最终所做的事情。谢谢!
    • 我漏掉了一行,刚刚添加了。最后一行将“Type”设置为“Redirect”。
    猜你喜欢
    • 2011-06-07
    • 2014-10-11
    • 2012-02-22
    • 2018-08-18
    • 2019-12-09
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多