IIS URL重写规则 [英] IIS URL Rewrite Rules
问题描述
我有一个AngularJS应用程序,它利用URL重写进行链接。我的重写规则如下:
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>
我在SO上找到了这个解决方案:如何在HTML5模式下为URL重写AngularJS应用程序配置IIS?并进行了一次修改以允许我的API通过。
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.
基本上,我想将我的所有请求重定向到 Default.cshtml
EXCEPT调用我的Web API。当我在基本URL上加载应用程序时,这非常有效: localhost / myapp
。但是,如果我在角度路线上重新加载页面,例如: localhost / myapp / dashboard
,它就会失败说:
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>
我在附近工作:
<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?
推荐答案
我需要更改模式并更改<在几个地方code> {REQUEST_FILE} 到 {REQUEST_URI}
。在这里查看我的演示:
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>
这篇关于IIS URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!