【问题标题】:React Router not functional when deployed in a sub-directory On ApacheReact Router 在 Apache 上的子目录中部署时不起作用
【发布时间】:2020-04-02 17:02:15
【问题描述】:

我在将我的 react 应用程序部署到 appache 上的子目录时遇到了相当大的问题。 我正在使用 create-react-app 和 React 路由器 v4。 当我访问网址 www.example.com/myApp 时。当我的主页正确呈现时,我正在呈现的 html。

但是,如果我尝试通过在地址栏中手动输入路线来访问注册页面,我会收到 404 Not found 错误。

我做了什么。

  1. 我已将 BrowserRouter 的 basname 设置为子目录。
  2. 我还在 package.json 中设置了主页属性。
  3. 我已经创建了 .htaccess 文件。

请我这几天一直在试图弄清楚我做错了什么,但我似乎无法弄清楚。

【问题讨论】:

    标签: reactjs apache react-redux react-router subdirectory


    【解决方案1】:

    Apache 尝试为请求的路由查找资源。您需要将所有请求重定向到您的索引,以便可以加载反应包并且反应路由器可以做它的事情

    这个问题已经解决了几次,所以我会参考其他有用的答案

    通过添加以下 Rewrite* 行来更改 VirtualHost 配置(通常在 /etc/httpd/conf.d\vhosts.conf 中找到):

    <VirtualHost *:8080>
      ServerName example.com
      DocumentRoot /var/www/httpd/example.com
    
      <Directory "/var/www/httpd/example.com">
        ...
    
        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]
      </Directory>
    </VirtualHost>
    

    SOURCE

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 2022-06-26
      • 2016-12-05
      • 2017-11-13
      • 2012-09-07
      • 1970-01-01
      • 2012-09-01
      • 2016-08-30
      相关资源
      最近更新 更多