【问题标题】:IIS Rewrite removing .html resulting in 404 Not FoundIIS 重写删除 .html 导致 404 Not Found
【发布时间】:2018-06-02 23:10:45
【问题描述】:

我正在使用 ASP.NET Web API 并尝试将带有 .html (http://www.example.com/api/TestPlay/Main/Authenticate.html) 的 URL 重写为没有 (http://www.example.com/api/TestPlay/Main/Authenticate) 的 URL,该 URL 路由到名为“TestPlay”的区域、名为“MainController”的控制器和操作命名为“认证”。但是,我得到的只是 404 Not Found。

我已经在我的 web.config 中安装了 URL Rewrite Module 2.1 和以下代码。还有什么我应该做的吗?

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Test Rewrite" stopProcessing="false">
                <match url="(.*)/api/TestPlay/(.*).html(.*)" />
                <action type="Rewrite" url="{R:1}/api/TestPlay/{R:2}" />
            </rule>
        </rules>
    </rewrite>
    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="TRACEVerbHandler" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

【问题讨论】:

    标签: asp.net-web-api url-rewrite-module iis-10


    【解决方案1】:

    您的规则中的正则表达式是错误的。正确的规则应该是:

    <rule name="Test Rewrite" stopProcessing="false">
        <match url="^api/TestPlay/(.*).html" />
        <action type="Rewrite" url="/api/TestPlay/{R:1}" />
    </rule>
    

    因为&lt;match url=" 中的正则表达式应该是请求路径的正则表达式,而不需要开始斜杠。例如,在您的情况下,URL 重写会将此字符串 api/TestPlay/Main/Authenticate.html 与正则表达式 ^api/TestPlay/(.*).html 进行比较

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 2018-12-27
      • 2019-12-10
      相关资源
      最近更新 更多