Search code examples
asp.net-web-apiurl-rewrite-moduleiis-10

IIS Rewrite removing .html resulting in 404 Not Found


I am using ASP.NET Web API and trying to rewrite an URL with .html (http://www.example.com/api/TestPlay/Main/Authenticate.html) to one without (http://www.example.com/api/TestPlay/Main/Authenticate) which routes to an Area named "TestPlay", Controller named "MainController" and Action named "Authenticate". However, all that I'm getting is 404 Not Found.

I have installed URL Rewrite Module 2.1 and the following codes in my web.config. Is there anything else I should do?

<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>

Solution

  • Regexp in your rule is wrong. Correct rule should be:

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

    Because regexp in <match url=" should be regexp for request path without starting slash. For example in your case URL rewrite will compare this string api/TestPlay/Main/Authenticate.html with regexp ^api/TestPlay/(.*).html