简体   繁体   中英

IIS rewrite condition for React MVC App

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM