使用 web.config 将根域重写为不同的路径然后子域

Rewrite root domain to different path then subdomain with web.config

我认为这应该是一个很容易的任务,但我不知何故在这上面挣扎。

我想做的事情:

mydomain.ch -> should redirect to a simple html page in folder public
demo.mydomain.ch -> should redirect to a angular application
demo.mydomain.ch/api -> is the backend (express.js) for the angular application

我的folder/file结构

web.config
demo
-> simplepage.html
-> index.html
-> angular.js
-> ...
backend
-> server.js

这是我当前的规则集:

<rule name="mydomain.ch rule"  stopProcessing="true">
    <match url="^mydomain\.ch$" />
    <action type="Rewrite" url="demo/simplepage.html"/>
</rule>

<rule name="Root rule">
    <match url="^$" />
    <action type="Rewrite" url="demo/index.html"/>
</rule>

<!-- For static files, redirect to the URI -->
<rule name="Static files">
    <action type="Rewrite" url="demo{REQUEST_URI}"/>
</rule>

<!-- For Express.js middleware API, if using api/ prefix, then use server.js -->
<rule name="Express.js URIs">
    <match url="api/*"/>
    <action type="Rewrite" url="backend/server.js"/>
</rule>

<!-- For Angular.js URIs, rewrite to the index.html file -->
<rule name="Angular">
    <match url=".*"/>
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
    </conditions>
    <action type="Rewrite" url="demo/index.html"/>
</rule>

目前发生的情况是,每次调用 mydomain.ch 或 demo.mydomain.ch 都会重定向到 angular 应用程序。我必须更改什么才能将 mydomain.ch 重定向到 simplepage.html?

如果您将规则更改为这样的内容,这将起作用:

<rule name="mydomain.ch rule"  stopProcessing="true">
    <match url="^$" /> 
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^mydomain\.ch$" />
    </conditions>
    <action type="Rewrite" url="demo/simplepage.html"/>
</rule>