javascript - React 应用返回 500 Internal Server Error

标签 javascript reactjs react-router create-react-app

我有一个 React 应用程序,是使用 create-react-app 创建的。

在我运行 npm run build 并将应用程序部署为静态站点后,一切正常,除了当我在索引以外的路由上刷新页面时,它失败并显示 500内部服务器错误

下面是我的路由器的代码。使用 react 路由器。

<Router history={browserHistory}>
  <Route path="/">
      <IndexRoute component={Login}/>
      <Route path="/dashboard" component={Dashboard}></Route>
      <Route path="/profile" component={Profile}/>
</Router>

感谢任何帮助。

最佳答案

当您直接访问您应用的某个路径时,例如通过https://myapp.com/route/123 Apache 尝试将该 URL 映射到公用文件夹中的文件。在这种情况下,它会查找显然不存在的/route/123.html。 为避免这种情况,我们必须告诉 Apache 将所有请求重定向到我们的 index.html,以便我们的应用程序可以对该 URL 执行一些路由魔术。 要告诉 Apache 将请求重定向到我们应用程序所在的 index.html,我们必须修改 .htaccess 文件。如果您应用的文件夹中还没有这样的文件,请创建它。

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

更多信息请查看this出。

关于javascript - React 应用返回 500 Internal Server Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42972073/

相关文章:

javascript - 根据地址获取经纬度

javascript - svg onload 函数什么时候发生

reactjs - react 路由器获取完整的当前路径名

javascript - 无法读取未定义的属性 'location'

javascript - 在 Angular 中使用 Web Worker(多线程)和在 NativeScript 中使用 Typescript 是否可行?

WebStorm:如何将 jsx 文件类型添加到 'File > New' 列表

javascript - 有没有办法将输入标签的 id 作为 onclick 函数参数传递,而无需在 JSX 中实际写入 id 值?

javascript - 重定向到新的外部 url 时如何保留 localStorage

javascript - React 路由组件类型错误 : history is undefined

reactjs - 错误 : useRoutes() may be used only in the context of a <Router> component