我正在尝试使用 BrowserRouter 让我的路由与 IIS 和 React Router v4 一起使用。
在我的本地主机上,我的所有路由都按预期工作。 React Router 按照我想要的方式处理一切:
- http://www.example.com/appfolder/dest http://www.example.com/appfolder/dest
- http://www.example.com/appfolder/dest/CODE http://www.example.com/appfolder/dest/CODE
- http://www.example.com/appfolder/dest/CODE/INVALID/URL http://www.example.com/appfolder/dest/CODE/INVALID/URL
现在,在 IIS 中,我设置了一条忽略“/appfolder/api/*”的规则,以便为我的应用程序提供 api。这样可行。
如何让 IIS 重定向到 'http://www.example.com/appfolder/dest/CODE http://www.example.com/appfolder/dest/CODE' 并让 React Router 根据其规则处理它。如果我重定向到“/appfolder/index.html”,我就会丢失“代码”,我想保留它以供 ReactRouter 处理。
如果我尝试重定向到 'http://www.example.com/appfolder/dest/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>
我找到了解决方案。我将动作类型更改为重写,效果非常好。
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)