question

MikeC-6297 avatar image
0 Votes"
MikeC-6297 asked SamWu-MSFT edited

IIS Rewrite : redirect Https one site in applicationHost.config

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[@name='$ruleName']/match" -name "url" -value "(.*)"
Add-WebConfigurationProperty -pspath $pspath -filter "system.webserver/rewrite/Rules/rule[@name='$ruleName']/conditions" -name "." -value @{input='{HTTPS}'; pattern='^OFF$'}
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@name='$ruleName']/action" -name "type" -value "Redirect"
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@name='$ruleName']/action" -name "url" -value "https://{HTTP_HOST}/{R:1}"
Set-WebConfigurationProperty -pspath $pspath -filter "system.webServer/rewrite/Rules/rule[@name='$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


windows-server-iis-configuration
sites.jpg (12.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SamWu-MSFT avatar image
0 Votes"
SamWu-MSFT answered SamWu-MSFT edited

Hi @MikeC-6297

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.