reactjs - 带有 React Router v4 的 IIS 规则

标签 reactjs iis url-rewriting react-router

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

在我的本地主机上,我的所有路由都按预期工作。 React Router 像我想要的那样处理一切:

  • http://www.example.com/appfolder/dest
  • http://www.example.com/appfolder/dest/CODE
  • http://www.example.com/appfolder/dest/CODE/INVALID/URL

  • 现在,在 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>
    

    最佳答案

    我找到了解决方案。我将我的操作类型更改为 Rewrite,它的作用就像一个魅力。

    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>
    

    关于reactjs - 带有 React Router v4 的 IIS 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172640/

    相关文章:

    javascript - 在 map 中显示子模式的最佳方式?

    javascript - 按 `z-index` 切换 div 时连续滚动

    javascript - 模态 - 保持默认值

    c# - jsreport - C# 中的访问被拒绝消息

    php - url 重写并传递表单的值

    html - 指向特定 Angular 2.0 SPA 状态的外部链接不起作用

    .htaccess - 删除所有 URL 的尾部斜杠

    javascript - React PropTypes - 以数字为键的形状

    c# - 如何从 IIS 上托管的 .net 代码访问 key 保管库 secret

    c# - 在 IIS 中枚举应用程序池