Skip to content

Instantly share code, notes, and snippets.

@brandanmajeske

brandanmajeske/web.config

Last active Sep 28, 2018
Embed
What would you like to do?
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>
@brandanmajeske

This comment has been minimized.

Copy link
Owner Author

@brandanmajeske brandanmajeske commented Mar 10, 2015

IIS web.config rewrite rule for MVC/WebAPI with AngularJS in Html5 Mode

@csfanse

This comment has been minimized.

Copy link

@csfanse csfanse commented Jun 10, 2016

Above code does not work in my case. I have created another web site in IIS(Version 10) other than default web site. The site contains two project. One is portal and other is Web API consumed in that portal. I am using angular components and component routing as well,

@crazyjat

This comment has been minimized.

Copy link

@crazyjat crazyjat commented Jan 3, 2018

With Owin 3.1.0 and IIS Express from Visual Studio 2015 this rewrite rule causes all of my static files to be rewritten to the index.html file.

Is there a way to fix that?

Here is my complete web.config file:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.6" />
    <httpRuntime targetFramework="4.6" />
  </system.web>
  
  <!-- Configure a transparent IIS: No static files, no handlers. -->
  <system.webServer>
    <!-- runAllManagedModulesForAllRequests: Make sure that we have OWIN handle static files, too. -->
    <modules runAllManagedModulesForAllRequests="true" />
    
    <rewrite>
      <rules>
        <rule name="html5mode" 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>
    
    <!-- Disable all static content handling in the IIS -->
    <staticContent>
      <clear />
    </staticContent>
    
    <!-- Remove all handlers -->
    <handlers>
      <clear />
    </handlers>
  </system.webServer>
  
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
@crazyjat

This comment has been minimized.

Copy link

@crazyjat crazyjat commented Jan 4, 2018

I found that the following rewrite rule works for a Owin 3.1.0 WebApi SPA Template project hosted with IIS Express from Visual Studio 2015. Since this project template uses a public folder to store all of the html the above rewrite rule will not work since the {REQUEST_FILENAME} variable does not include the public folder in the path. For example, if my project is located at c:\testapp and my project folder contains a public folder (so c:\testapp\public) that contains my html, css and javascript and the browser requests index.js the {REQUEST_FILENAME} variable will be c:\testapp\index.js. Why this happens makes no sense, but I have verified that it does with trace logging. To fix my issue I had to use this rewrite rule.


    <rewrite>
      <rules>
        <rule name="Main Rule" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{APPL_PHYSICAL_PATH}public{REQUEST_URI}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{APPL_PHYSICAL_PATH}public{REQUEST_URI}" matchType="IsDirectory" ignoreCase="true" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
@digrizzz

This comment has been minimized.

Copy link

@digrizzz 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" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.