IIS URL重写改为重定向

时间:2014-12-01 22:44:25

标签: iis url-rewriting url-rewrite-module

目标

使用IIS 7.5和URL Rewrite 2.0模块,我想在没有浏览器重定向的情况下将相对路径“/ myapp”重写为“/ myapp-public”。

我尝试了什么

我在wwwroot中放置了以下web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
    </system.web>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite to public" stopProcessing="true">
                    <match url="^myapp" />
                    <action type="Rewrite" url="/myapp-public" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

问题

IIS不会在服务器端重写URL,而是以“301 Moved Permanently”响应,将用户重定向到“/ myapp-public”。它表现得好像我使用了“Redirect”类型的操作而不是“Rewrite”。

有谁知道为什么“重写”操作会执行重定向?有关如何进一步调试此问题的任何想法?谢谢!

1 个答案:

答案 0 :(得分:9)

您正在使用IIS为您执行的礼貌文件夹重定向,请参阅此处:http://support.microsoft.com/kb/298408/EN-US。要解决此问题,请将尾部斜杠添加到重写URL中,如下所示:

<action type="Rewrite" url="/myapp-public/" />

但是,如果这些实际上是您的实际网址,那么您还有其他问题。您的匹配网址将在您的重写网址上匹配,并会导致无限循环,因为^ myapp与myapp-public匹配。您可以将字符串($)的结尾添加到您的匹配网址以修复此问题,即^ myapp $以防止myapp-public在其上进行匹配。

相关问题