如何在 IIS 中用斜杠重写 URL

How to rewrite URL with trailing slash in IIS

我目前遇到问题,我想从我的 IIS web.config 文件的根文件夹中重写一个 URL。我的配置现在看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="phpmyadmin" stopProcessing="true">
                    <match url="(.*)phpmyadmin(.*)" />
                </rule>
                <rule name="redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="/test/{REQUEST_URI}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

也安装了 phpMyAdmin,正如您在配置中看到的那样。它将被分开,因为我确实有以下文件结构:

当用户输入 URL 时,例如 http://localhost/,他实际上应该重写为 http://localhost/test。如果他输入 http://localhost/subtest/ 那么他应该重写为 http://localhost/test/subtest.

这已经适用于脚本。但现在的问题是,我无法键入 http://localhost/subtest(尾部斜线丢失),也无法重写 http://localhost/test/subtest。相反,我被重定向到 http://localhost/test/subtest,当然它找不到该文件夹​​,因为根目录仍然来自 test 文件夹。

有人可以帮助我吗?

提前致谢。

经过多次尝试,我现在有了解决方法。现在我正在检查是否首先使用测试文件夹调用了 URL。如果是这种情况,它将重定向到正确的 URL.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="phpmyadmin" stopProcessing="true">
                    <match url="(.*)phpmyadmin(.*)" />
                </rule>
                <rule name="redirect" stopProcessing="true">
                    <match url="^test/(.*)" />
                    <action type="Redirect" url="/{R:1}" />
                </rule>
                <rule name="rewrite" stopProcessing="true">
                    <match url="^(.*)" />
                    <action type="Rewrite" url="/test/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>