【问题标题】:URL rewrite for Nested URL in ReactJSReactJS 中嵌套 URL 的 URL 重写
【发布时间】:2020-08-25 10:36:05
【问题描述】:

我在 IIS 上运行 react 应用程序,我使用 URL 重写进行路由配置,这是我的 web.config 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <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="^/(docs)" negate="true" />
              </conditions>
              <action type="Rewrite" url="index.html" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

此代码适用于 localhost/ReactApp/User 等 url, 现在我希望它像 localhost/ReactApp/User/Edit 一样工作,但它不适用于显示空白页面的子页面, 此问题仅在构建和部署之后出现,请告诉我它如何适用于 /User/Edit 嵌套页面或子页面?

【问题讨论】:

  • 我有同样的问题,但我无法让任何路线工作,你在哪里使用这个 web.config?其他目录中是否还有其他 web.config?

标签: reactjs iis url-rewriting


【解决方案1】:

当您从子文件夹中提供 react 应用程序时,您需要在路由中设置基本名称 &lt;BrowserRouter basename='/path/to/subfolder/'&gt;

试试下面的iis url重写规则:

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{DOCUMENT_ROOT}{URL}" matchType="IsFile" ignoreCase="false" />
        <add input="{DOCUMENT_ROOT}{URL}" matchType="IsDirectory" ignoreCase="false" />
      </conditions>
      <action type="None" />
    </rule>
    <!--# Fallback all other routes to index.html-->
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^" ignoreCase="false" />
      <action type="Rewrite" url="/path/to/subfolder/index.html" />
    </rule>
  </rules>
</rewrite>

【讨论】:

  • basename 的路径应该像 /appfolder/app/build/ 吗?到构建文件夹?
  • @Erik 在站点文件夹中誓言您必须选择构建文件夹,但在 URL 重写规则中,您可以设置索引文件。如果索引文件在子文件夹中,则添加子文件夹 oath.
猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多