【问题标题】:Error trying to create Url Rewrite Rule from Azure Devops尝试从 Azure Devops 创建 URL 重写规则时出错
【发布时间】:2019-09-16 06:28:56
【问题描述】:

我正在使用 azure devops 步骤尝试使用管理 IIS 任务在 IIS 中创建 https 重定向规则。

我正在使用以下“其他 appcmd.exe 命令”

set config -section:system.webServer/rewrite/rules /+"[name='http_redirect',enabled='True']" /commit:apphost

set config -section:system.webServer/rewrite/rules.[name='http_redirect'] /match.url:"(.*)" /match.ignoreCase:true /commit:apphost

set config -section:system.webServer/rewrite/rules.[name='http_redirect'].conditions/add /+"[input='{HTTPS}',pattern='off']" /commit:apphost

set config -section:system.webServer/rewrite/rules.[name='http_redirect'].action /+"[type='Redirect',url='https://{HOST_NAME}/{R:1}',redirectType='Found']" /commit:apphost

似乎第一个命令在创建空白规则时运行,但第二个命令尝试通过名称 http_redirect

查找规则失败

【问题讨论】:

    标签: iis https azure-devops


    【解决方案1】:

    您可以使用以下命令全局设置 URL 重写:

    appcmd.exe set config  -section:system.webServer/rewrite/globalRules /+"[name='http_redirect']" /commit:apphost
    appcmd.exe set config  -section:system.webServer/rewrite/globalRules /[name='http_redirect'].match.url:"(.*)"  /commit:apphost
    
    appcmd.exe set config  -section:system.webServer/rewrite/globalRules /+"[name='http_redirect'].conditions.[input='{HTTPS}',pattern='off']" /commit:apphost
    appcmd.exe set config  -section:system.webServer/rewrite/globalRules /[name='http_redirect'].action.type:"Redirect" /[name='http_redirect'].action.url:"https://{HTTP_HOST}/{R:1}" /[name='http_redirect'].action.redirectType:"Found"  /commit:apphost
    

    注意:不要使用 {HOST_NAME},使用 {HTTP_HOST}。

    申请特定站点使用此命令:

    appcmd.exe set config "aspsite" -section:system.webServer/rewrite/rules /+"[name='http_redirect']" /commit:apphost
    
    
    appcmd.exe set config "aspsite" -section:system.webServer/rewrite/rules /[name='http_redirect'].match.url:"(.*)"  /commit:apphost
    
    
    appcmd.exe set config "aspsite" -section:system.webServer/rewrite/rules /+"[name='http_redirect'].conditions.[input='{HTTPS}',pattern='off']" /commit:apphost
    
    
    appcmd.exe set config "aspsite" -section:system.webServer/rewrite/rules /[name='http_redirect'].action.type:"Redirect" /[name='http_redirect'].action.url:"https://{HOST_NAME}/{R:1}" /[name='http_redirect'].action.redirectType:"Found"  /commit:apphost
    

    编辑: power-shell 命令添加规则:

    import-module webAdministration
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webserver/rewrite/GlobalRules" -name "." -value @{name='HTTP to HTTPS Redirect'; patternSyntax='ECMAScript'; stopProcessing='True'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webserver/rewrite/GlobalRules/rule[@name='HTTP to HTTPS Redirect']/match" -name url -value "(.*)"
    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webserver/rewrite/GlobalRules/rule[@name='HTTP to HTTPS Redirect']/conditions" -name "." -value @{input="{HTTPS}"; pattern='^OFF$'}
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules/rule[@name='HTTP to HTTPS Redirect']/action" -name "type" -value "Redirect"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules/rule[@name='HTTP to HTTPS Redirect']/action" -name "url" -value "https://{HTTP_HOST}/{R:1}"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules/rule[@name='HTTP to HTTPS Redirect']/action" -name "redirectType" -value "SeeOther" 
    

    【讨论】:

    • 谢谢,为了使这个可重复,如果规则已经存在,我需要先删除它,我会使用什么命令?
    • 您可以使用此命令删除现有规则:appcmd.exe set config -section:system.webServer/rewrite/globalRules /-"[name='http_redirect']" /commit:apphost
    • 有什么方法可以编辑值而不是使用 + 或 -?
    • 在我看来,这是一种方式,你需要做一些研究才能找到另一种方式。您还可以使用 PowerShell 命令添加规则。您在创建规则时遇到 + 或 - 的任何问题吗?
    猜你喜欢
    • 2020-08-20
    • 2021-08-12
    • 2022-01-02
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多