【问题标题】:Apache 2.4 + React Router 4 always routing to my 404 page componentApache 2.4 + React Router 4 总是路由到我的 404 页面组件
【发布时间】:2019-01-08 14:09:55
【问题描述】:

我无法让我的 create react 应用程序构建正确路由到我服务器上的组件。

我有一个 .htaccess 文件

RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]

所以它总是路由到我的 index.html

我网站的网址是 .com/project,我的 package.json 文件中的主页选项是 homepage: '/project'

我已经无事可做。

因为我的 htaccess 它总是路由到我的 index.html 但路由器总是加载 404 组件。我错过了什么。

【问题讨论】:

    标签: reactjs apache react-router create-react-app


    【解决方案1】:

    发生这种情况是因为基本路由错误,因此一直路由到 404 页面。

    设置 <Router basename="/<Directory>"> 并将主页选项添加到我的 package.json 文件解决了这个问题。

    【讨论】:

      【解决方案2】:

      对我来说,使用这个 .htaccess:

      RewriteEngine On
      RewriteBase /project
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-l
      RewriteRule ^(.*)$ index.html?$1 [L,QSA]
      

      【讨论】:

        猜你喜欢
        • 2019-05-24
        • 2017-12-08
        • 1970-01-01
        • 1970-01-01
        • 2020-06-21
        • 1970-01-01
        • 1970-01-01
        • 2020-01-03
        • 1970-01-01
        相关资源
        最近更新 更多