【问题标题】:React Router Gives 404 error on productionReact Router 在生产中出现 404 错误
【发布时间】:2018-06-09 04:16:56
【问题描述】:

React Router 可以在本地机器、Chrome 上运行,但不能在 safari 和其他浏览器上运行,

 <Switch>
    <Route exact path={"/"} component={Home} />
    <Route path="/programs/:slug" component={Programs} />
    <Route path="/page/:slug" component={Page} />
    <Route component={NotFound} />
</Switch>

这是我的代码,但它在开发模式下完美运行,没有错误,但在生产中,当我导航到程序/:slug 或 page/:slug 时,safari 会出现 404 错误

【问题讨论】:

  • 您是否将 url rewrite 用于生产。所有网址都应定位到index.html
  • 是这个吗?我在构建“主页”之前将它放在我的 package.json 中:“example.co
  • 顺便说一句,我正在使用共享主机
  • 在生产中,您的应用程序在作为单页应用程序托管的 nodejs 上热运行。在生产中,您需要放置一些依赖于服务器的配置。就像在 apache 中一样,您可以在 .htaac​​cess 中进行配置,或者在 nginx 中进行重写。
  • 我对 botmap.io 不熟悉。我已经回答了。如果您找到答案,请标记为完成。

标签: javascript reactjs react-router


【解决方案1】:

我将此添加到 .htaccess

RewriteBase /
RewriteCond %{REQUEST_URI} !^/(assets/?|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

感谢 Kishan Mundha 的解释

【讨论】:

    【解决方案2】:

    您可能需要在服务器上配置单个入口点index.html。所有 url 都应该指向index.html,入口点将根据路由决定如何渲染和内容。

    【讨论】:

    • 怎么做?
    【解决方案3】:

    如果您使用的是pm2 serve,则只需添加--spa 标志即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2018-10-21
      • 2019-04-20
      • 2019-07-07
      相关资源
      最近更新 更多