IIS Rewrite : redirect Https one site in applicationHost.config

MikeC 1 Reputation point
2021-09-21T13:27:39.523+00:00

Hello,

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

133974-sites.jpg

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 :

$pspath = "MACHINE/WEBROOT/APPHOST/"

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"

OR

xml file :

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

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?

Thks

Internet Information Services
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sam Wu-MSFT 7,536 Reputation points Microsoft Vendor
    2021-09-22T02:26:10.66+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.