IIS rewrite condition for React MVC App

蓝咒 提交于 2019-12-13 03:56:41

问题


I have created a create-react-app and am trying to publish to a production site.

My goal is to have all calls to /api/* go through to the MVC Server application's controller. Also serve any static files from /content/* and /static/*

All other calls should serve the 1 file /index.html (which is where my react files live and they will handle the routing.

I am using the IIS Rewrite module, and I am trying to use the requested URL "Does Not MAtch the pattern" with regexp.

MY current IIS setup ignores the "do not rewrite if pattern matches" and rewrites everything to index.html

Here is the IIS setup:

and here is the web.config. I'm not sure what I am doing wrong.

 <rewrite>
            <rules>
                <rule name="React Rewrite" enabled="true" patternSyntax="ECMAScript">
                    <match url="(.*)/api/(.*)|(.*)/content/(.*)|(.*)/static/(.*)" negate="true" />
                    <action type="Rewrite" url="/index.html" />
                    <conditions>
                    </conditions>
                </rule>
            </rules>
        </rewrite>

回答1:


I've solved it by using the Conditions. My solution is:

<rewrite>
            <rules>
                <rule name="React Rewrite" enabled="true" patternSyntax="ECMAScript">
                    <match url="(.*)" negate="false" />
                    <action type="Rewrite" url="/index.html" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_URI}" pattern="^(/api/)" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^(/content/)" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^(/static/)" negate="true" />
                    </conditions>
                </rule>
            </rules>
        </rewrite>


来源:https://stackoverflow.com/questions/49594026/iis-rewrite-condition-for-react-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!