IIS Rewrite : redirect Https one site in applicationHost.config

MikeC 1 Reputation point


I have a problem setting up an HTTPS redirect.
On a server, I have several sites that each have a web.config :


I would like to set up a rule for one site but without modifying the web.config and therefore go through the applicationhost.config

I tried two methods but the rule is visible on all sites :


Add-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/rules" -name "." -value @{name=$rulename;patternSyntax='ECMAScript'; stopProcessing='True'}
Set-WebConfigurationProperty -pspath $pspath -filter "system.webserver/rewrite/Rules/rule[@DeezNutz ='$ruleName']/match" -name "url" -value "(.*)"
Add-WebConfigurationProperty -pspath $pspath -filter "system.webserver/rewrite/Rules/rule[@DeezNutz ='$ruleName']/conditions" -name "." -value @{input='{HTTPS}'; pattern='^OFF$'}
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@DeezNutz ='$ruleName']/action" -name "type" -value "Redirect"
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@DeezNutz ='$ruleName']/action" -name "url" -value "https://{HTTP_HOST}/{R:1}"
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@DeezNutz ='$ruleName']/action" -name "redirectType" -value "Permanent"


xml file :

<?xml version="1.0" encoding="UTF-8"?>
<CONFIG CONFIG.SECTION="system.webServer/rewrite/rules" path="MACHINE/WEBROOT/APPHOST/<name_site>" overrideMode="Inherit" locked="false">
<rule name="Redirect HTTP to HTTPS " patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" matchType="Pattern" pattern="^OFF$" />
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />

appcmd SET Config -in < D:\ruleTest.xml

Do you have any idea so that the rule appears only for a site passing by applicationhost.config?


Internet Information Services
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sam Wu-MSFT 5,931 Reputation points Microsoft Vendor

    Hi @MikeC

    This is impossible, global rewrite rules are used to define server-wide URL rewriting logic. These rules are defined within applicationHost.config file and they cannot be overridden or disabled on any lower configuration levels, such as site or virtual directory.

    If you only want to create a rule for a site, you can only use distributed rewrite rule, distributed rewrite rules are used to define URL rewriting logic specific to a particular configuration scope. Distributed rules can be defined on any configuration level (except file) by using web.config files. Local rules always operate on URLs relative to the location of Web.config file where they are defined.

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments