Skip to content

Instantly share code, notes, and snippets.

@brandanmajeske
Last active January 5, 2021 08:49
Show Gist options
  • Save brandanmajeske/ec99043d9af49f2bd7c2 to your computer and use it in GitHub Desktop.
Save brandanmajeske/ec99043d9af49f2bd7c2 to your computer and use it in GitHub Desktop.
IIS web.config rewrite rule for MVC/WebAPI with AngularJS in Html5 Mode
<!-- $locationProvider.html5Mode(true) in app.js and <base href="/"> in head tag -->
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS" 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}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
@digrizzz
Copy link

digrizzz commented Sep 28, 2018

thanks @craztjat ! That was helpfull. Alswo i want to add for those from google like me: you may also should include rule for negating xhr (if you're adding scripts dynamically for example): <add input="{HTTP_X_Requested_With}" pattern="^XMLHttpRequest$" negate="true" />

@sascalas
Copy link

sascalas commented Jan 5, 2021

thanks @craztjat ! That was helpfull. Alswo i want to add for those from google like me: you may also should include rule for negating xhr (if you're adding scripts dynamically for example): <add input="{HTTP_X_Requested_With}" pattern="^XMLHttpRequest$" negate="true" />

Gracias compañero. Era lo que necesitaba para que terminara de funcionar mi aplicación con MVC + Angular. Copio finalmente como queda mi ReWrite:

   <rewrite>
   <rules>
     <!--Regla para que no filtre las llamadas a Swagger-->
     <rule name="OpenLIS.API.Swagger" stopProcessing="true" enabled="true">
       <match url="^(swagger)" ignoreCase="true" />
       <action type="None" />
     </rule>
     <!--Regla para que no filtre las llamadas a la API-->
     <rule name="OpenLIS.API.Rule" stopProcessing="true" enabled="true">
       <match url="^(api)"  ignoreCase="true"/>
       <action type="None" />
     </rule>
     <!--Regla para que si lo que se llama no existe (Ni es un archivo, ni es un directorio, ni es una llamada AJAX) entonces
     se redirija al root de angular, para que pueda rutear la applicación.
     Se añade regla XHR para que no filtre las llamadas desde AJAX que utilizan .js que no controlamos como jQuery. 
     Usa el objeto XMLHttpRequest(XHR) para hacer llamadas API del lado del servidor 
     desde el navegador.-->
     <rule name="OpenLIS.Angular.Rule" stopProcessing="true" enabled="true">
       <match url="(.*)" />
       <conditions logicalGrouping="MatchAll">
         <add input="{HTTP_X_Requested_With}" pattern="^XMLHttpRequest$" negate="true" />
         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
       </conditions>
       <!--<action type="Redirect" url="." /> -->
       <action type="Rewrite" url="." />
     </rule>
   </rules>
</rewrite>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment