Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite Rules

I have a AngularJS application that utilizes URL Rewriting for linking. My rewrite rule looks like :

<rewrite>
<rules> 
  <rule name="MainRule" 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_FILENAME}" matchType="Pattern" pattern="^api/(.*)" negate="false" />
    </conditions>
    <action type="Rewrite" url="Default.cshtml" />
  </rule>
</rules>

I found this solution on SO: How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? and made one modification to allow my API to pass through.

Essentially, I want to redirect all my requests to Default.cshtml EXCEPT calls to my Web API. This works GREAT when I'm I load the app on the base URL like: localhost/myapp. However, if I reload the page on a angular route like: localhost/myapp/dashboard it fails saying:

<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost/myapp/dashboard'.</Message>
<MessageDetail>No type was found that matches the controller named 'dashboard'.</MessageDetail></Error>

I have a work around where I did:

 <rewrite>
  <rules>
    <rule name="Default" stopProcessing="true">
      <match url="^(?!lib|api|dist|assets|app/|bower|common|main|signalr|templates|bower_components/).*" />
      <action type="Rewrite" url="Default.cshtml" />
    </rule>
  </rules>
</rewrite>

and it worked fine. Any ideas how I can utilize a cleaner solution above and still accomplish the rewrite?

like image 606
amcdnl Avatar asked Sep 03 '14 12:09

amcdnl


People also ask

How does IIS URL Rewrite work?

The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.

Where are IIS rewrite rules stored?

When done on the server level it is saved in the ApplicationHost. config file. You can also define it on the folder level, it that case it is saved in a web. config file inside that folder.

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.


1 Answers

I need to change the pattern and change {REQUEST_FILE} to {REQUEST_URI} in a couple of places. See my demo here:

<rewrite>
  <rules>
    <rule name="MainRule" 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" />
        <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
      </conditions>
      <action type="Rewrite" url="Default.cshtml" />
    </rule>
  </rules>
</rewrite>
like image 187
amcdnl Avatar answered Sep 28 '22 02:09

amcdnl