首页 > 解决方案 > 尝试从 Azure Devops 创建 URL 重写规则时出错

问题描述

我正在使用 azure devops 步骤尝试使用 Manage 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查找规则失败

标签: iishttpsazure-devops

解决方案


您可以使用以下命令全局设置 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" 

推荐阅读