Why do I get 405 error after adding a rewrite rule? - iis

I need the rewrite rule for being able to refresh a webpage (based on AngularJS), without getting 404 error - and this is working perfect. However this is causing 405 error for any method except GET (deploy to local IIS).
** WebDAV is not enabled **
Below is the rule which I am adding to the web.config file:
<rewrite>
<rules>
<rule name="Redirect to index.html" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
Any ideas?

Related

Hidden redirect IIS - web.config

I have an Windows Server hosted (IIS), in the root dir (head dir with FTP) I have a directory /www/ with an Angular SPA built, so an index.html inside.
I want to say to the server:
"Ehi, when people go to https://example.com you must to show www/index.html but with https://example.com on the URL" (so not with https://example.com/www/) is this possible??
And i have to say also:
"Ehi server! The routes are not real directory! This is an SPA!!" becouse the routing navigation is OK but the refresh doesn't works.
I think i have to write something on web.config file, but what?
Thanks in advance!
UPDATE:
I'm using:
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS Routes" 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="/www/" />
</rule>
</rules>
</rewrite>
</system.webServer>
This works for refresh but for "hidden redirect"...

IIS Rewriting: Why it not returning fresh data

I am using the following rewriting rule to get the data from the other server, and it is returning the result for the first time. But when I am updating/adding the data, my API is still returning the previous data(no updated data). But after approx 30 seconds when I again hit the API, It returns updated data.
It's working fine when I am using a direct URL(http://172.20.20.23:3000/).
I don't know what am I missing. Please help me to identify the issue.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Frontend Router" 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="^/(qosapi)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="^qosapi/(.*)" />
<action type="Rewrite" url="http://172.20.20.23:3000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
It was a reverse proxy caching issue. I just disabled the caching and it's working perfectly fine now.
See the below image:

How can I write IIS Server URL rewrite for specific route of angular 6 application that return Server Error 404?

I have hosted my angular 6 application on IIS server and there is a specific pattern that gets 404 error and the pattern is as follows:
http://localhost:4200/confirmatin/Id/token
that is
http://sandbox.app.com/confirmation/186d01b9-6984-4674-8a7f-fab03f0bd6da/CfDJ8McKuDKimzdIr7t2WefOlD1ZXQAD9C5q1aFKlmTCvVgfSvb8WdUCkGVUl5kvPvB2OdB1M2M4Ef6DMxX0b0b6cYZy7hxA%2B%2FpSyBuQbLFn0Mqw1T8mYkm6T66JCYw%2Bzp3qm0NBGuPlzo1%2BmhMePzNHb%2B1bR8rQS%2Bj8irqemy%2Bg5YJBTgyt1Cfch43zC5eneBaNu%2FNi8U5IwUT6gJLjznw%2Flutja48Jfe5DOkhlIvLWrGPNzbwZzLozqq3%2FjJIqFA88%2BQ%3D%3D
The token may contains any special characters such as /+-= etc.
If I removed the special character in the querystring, the pointer is passed to next phase with invalid token.
<system.webServer>
<rewrite>
<rules>
<rule name="angular routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
</system.webServer>
The angular route has following path set on routing-module.ts:
{path:'confirmation/:id/:token', component:ConfirmationComponent}
Could you please suggest what I am missing here?

IIS Rewrite rule for serverURL/Home/Index/2 URL type

I've implemented Angular 2 app with the routing, its working fine with the URL like serverURL/Home/Index its properly navigating to appropriate component without any errors.
But when I try entering serverURL/Home/Index/2 its giving me the following error
I've written below snippet in the web.config for Rewriting in IIS
<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" />
</conditions>
<action type="Rewrite" url="src/" />
</rule>
</rules>
</rewrite>
</system.webServer>
Even I've written { path: "Index/:id", component: IndexComponent }, in the routing file
Please help in this, thanks in advance.

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

Resources