Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up an IIS Rewrite rule for everything except the /api route

I'm setting up a webapi application with an angular front-end. I want to enable html5mode, so the web server needs to have a re-write rule that sends all requests back to the index page.

The problem is the actions in the /api route are matching this rule of course, so none of my REST calls will pass through the rewrite rule.

I'm sure I'm not alone with this problem. Can anyone share the section of the web.config so that my REST service works and html5mode isn't broken?

like image 919
Michael Draper Avatar asked Jul 18 '15 06:07

Michael Draper


People also ask

How do I create a redirect rule in IIS?

IIS Rewrite rule for static 301 page redirect The rewritemaps. config file contains a one-to-one mapping between an old URL and the new URL and looks like below. For StaticRedirects you need to use the first section with the name “StaticRedirects,” which is the same name as you have in your condition.

What is difference between routing and URL rewriting?

URL rewriting is focused on mapping one URL (new url) to another URL (old url) while routing is focused on mapping a URL to a resource. Actually, URL rewriting rewrites your old url to new one while routing never rewrite your old url to new one but it map to the original route.


1 Answers

  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <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>
    <rewrite>
      <rules> 
        <rule name="Main Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />

          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
like image 64
Michael Draper Avatar answered Sep 26 '22 01:09

Michael Draper