【问题标题】:How to enable React routing in IIS?如何在 IIS 中启用 React 路由?
【发布时间】:2018-10-15 10:26:06
【问题描述】:

以下组件是从我的 React 应用程序的 App.js 加载的。

const Main = () => (
  <BrowserRouter>
    <Switch>
      <Route exact path="/:userid" component={Banks} />
    </Switch>
  </BrowserRouter>
);

当应用程序使用本地开发服务器运行时,我可以从以下 url 获取用户 ID:

http://myAppPath/100

但是,当我构建应用程序并将其托管在 IIS 中时,我无法获取用户 ID。

https://localhost/Bank/100 --> 将 HTTP 更新为 HTTPS

路由无法识别。我使用了 URL Rewrite 但 userid 的值仍然没有进来。

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Routing" enabled="true" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="/" logRewrittenUrl="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

index.html:

<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="manifest" href="./manifest.json"><link rel="shortcut icon" href="./favicon.ico"><title>Bank App</title><link href="./static/css/main.b730bb13.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script type="text/javascript" src="./static/js/main.2564873f.js"></script></body></html>

【问题讨论】:

  • 将您的构建index.html unminified 添加到此问题
  • 你的 html 中没有基本标签

标签: reactjs iis routing


【解决方案1】:

首先从here 在您的服务器上安装iis url rewrite。 那么只有它尊重你的 url 重写配置

更新

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="/{R:1}"/>
        </rule>
        <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" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

【讨论】:

  • 我已经安装了 iis url rewrite。我只通过 IIS 管理器编写了规则。
  • 您在访问这些 url 时是否遇到 404 错误或服务器错误?
  • 404 - 找不到文件或目录。
  • 它仍然给出 - 404 - 找不到文件或目录。
  • 您的应用需要位于根文件夹中。它必须有 index.html 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 2021-12-25
  • 2020-09-08
相关资源
最近更新 更多