首页 > 解决方案 > 如何在 IIS 中用斜杠重写 URL

问题描述

我目前有一个问题,我想从我的 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文件夹中。

有人能帮我吗?

先感谢您。

标签: urliisurl-rewritingweb-config

解决方案


经过多次尝试,我现在找到了解决方法。现在我正在检查是否首先使用测试文件夹调用了 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>

推荐阅读