带有React Router v4的IIS规则

Gab*_*iel 1 iis url-rewriting reactjs react-router

我正在尝试使用BrowserRouter使我的路由与IIS和React Router v4一起使用。

在我的本地主机上,我的所有路由均按预期工作。React Router可以像我希望的那样处理所有事情:

现在,在IIS中,我设置了一个忽略'/ appfolder / api / *'的规则,目的是为我的应用程序提供api。这样可行。

如何使IIS重定向到“ http://www.example.com/appfolder/dest/CODE ”,并使React Router根据其规则进行处理。如果我重定向到'/appfolder/index.html',我会放开我想保留给ReactRouter处理的'CODE'。

如果我尝试使用正则表达式捕获组重定向到“ http://www.example.com/appfolder/dest/CODE ”,则会收到“ ERR_TOO_MANY_REDIRECTS”。

我的web.config中目前有以下规则:

    <rule name="ReactRouter 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="^/appfolder/api/" negate="true" />
      </conditions>
      <action type="Redirect" url="/appfolder/dest/CODE" />
      <!-- also tried this -->
      <!-- action type="Redirect" url="/appfolder/index.html" /-->
    </rule>
Run Code Online (Sandbox Code Playgroud)

Gab*_*iel 5

我找到了解决方案。我将动作类型更改为“重写”,它的工作原理很吸引人。

IIS规则最终解决方案:

<rule name="ReactRouter 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="^/appfolder/api/" negate="true" />
  </conditions>
  <action type="Rewrite" url="/appfolder/index.html" />
</rule>
Run Code Online (Sandbox Code Playgroud)