首页 > 解决方案 > 多个 SPA 的 IIS 重写规则

问题描述

我们正在为我们的微前端创建一个新架构,并且我们希望避免使用另一个框架来处理它,比如single-spa。我们公司目前将 IIS 用于其他产品,我们需要保持这种方式。

因此,为了让多个 SPA 响应同一个地址,我们只需将我们构建的 SPA 放在 IIS 的内容目录中,如下所示:

IIS 根文件夹:

我可以正常访问myhost/spa1myhost/spa2浏览他们自己的内部路线(我们使用的是 Angular)。

问题是,我们希望 URL 没有#,并且我们需要告诉 IIS 将任何子路由重定向到它的 SPA 主路由。

我们有一些东西,它适用于myhost/spa1, myhost/spa1/detail,但不适用于myhost/spa1/detail/1。比方说,第三级。

有人可以帮我解决这个问题吗?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Rule" stopProcessing="true">
                    <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="{R:1}/" />
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>

标签: angulariissingle-spa

解决方案


尝试使用此 URL 重写规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Rule" stopProcessing="true">
                    <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="{R:1}/{R:2}" />
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>

在此处输入图像描述


推荐阅读