在 Azure Web App 上使用 web.config 将子域重写到目录

Rewriting subdomain to directory with web.config on Azure Web App

我正在尝试将我的所有子域托管在我的网络服务器上的相应文件夹中,这意味着

https://foo.bar.com/index.html

位于

https://bar.com/foo/index.html

根据我当前的配置,该文件不能也不应该通过 "original" link 访问。

我当前的方法有效,除了 urls,它指向路径中没有文件名的子目录 - 在这种情况下,子域被重新包含在重写的路径中 url:

https://foo.bar.com/this/does/work.html

提供位于

的文件
https://bar.com/foo/this/does/work.html

同时

https://foo.bar.com/this/does/not/work

在浏览器中重写为

https://foo.bar.com/foo/this/does/not/work

由于显而易见的原因导致 404。

但是,如果您在浏览器中直接输入有问题的url

https://foo.bar.com/this/does/not/work

它按预期工作。

我知道我在宽泛意义上使用 "obviously" 这个词。这是一个快速演示:

https://egal.todoservice.app/

如您所见,页面上的 link https://egal.todoservice.app/sub/dir/

带你去 https://egal.todoservice.app/egal/sub/dir/

但请尝试直接在您的地址栏中输入 link 并且它有效 - 至少对我有效,在 Chrome、Edge 和 Firefox 中测试过。

更新: 奇怪的是,在 Whosebug 上使用子目录 link 按预期工作...

这是我的 web.config 负责子域重写的部分

<rule name="Rewrite Subdomain To Directory" stopProcessing="false">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
    </conditions>
    <action type="Rewrite" url="{C:1}/{R:0}" />
</rule>

这是我的全部 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <staticContent>
             <mimeMap fileExtension="." mimeType="text/plain" />
             <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
         </staticContent>        
        <rewrite>
            <rules>
                <rule name="Rewrite SMZC URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.sadmenszombieclub.com"  />
                    </conditions>
                    <action type="Rewrite" url="smzc/{R:0}" />
                </rule>
                <rule name="Rewrite imagiro URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.imagiro.net"  />
                    </conditions>
                    <action type="Rewrite" url="imagiro/{R:0}" />
                </rule>

                <rule name="Rewrite Subdomain To Directory" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}/{R:0}" />
                </rule>

                <rule name="Rewrite root path to service Directory" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="service/{R:0}" />
                </rule>                
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

整个站点托管为 Azure Web 应用程序。

从评论中复制。

您所做的不仅仅是简单的重写,而是反向代理。因此,需要进行一些更改,

  1. 在您的操作中,不要重写实际路径 (<action type="Rewrite" url="{C:1}/{R:0}" />),您还应该将完整的域名附加到 url。这样 IIS 就知道您要设置反向代理。
  2. 通过关注 https://tomssl.com/2015/06/15/create-your-own-free-reverse-proxy-with-azure-web-apps/
  3. 等博文,在 Azure 应用服务中启用 ARR 代理模式