未找到生产构建404的反应

Jur*_*jka 4 apache reactjs

您好我需要部署反应应用程序.

要实现我的运行:"npm run build"

之后在我的vhost.conf中添加了vhost

<VirtualHost *:80>
ServerName hello.local
DocumentRoot c:/wamp64/www/hello_world/build
<Directory  "c:/wamp64/www/hello_world/build">
             Options Indexes FollowSymLinks MultiViews
             AllowOverride All
             Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

我还添加了etc/hosts hello.local

当然我已经在httpd.conf中启用了mod重写

当我运行我的反应应用程序显示的hello.local /主页时,但是当我想要反应 - 反应路径路径hello.local/example我收到404未找到错误.请帮助它是什么?这是apache配置的问题还是反应路由器有一些错误?问候

Pan*_*her 12

这是SPA的常见问题.在SPA中,大多数路由都发生在客户端.在你的情况下,大多数react-router应该做的工作.由于整个js捆绑为单个文件并提供服务index.html,因此您需要为服务器中的index.html所有路径提供non-existing服务.

你必须添加这样的配置

RewriteEngine On  
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteRule ^ /index.html [L]
Run Code Online (Sandbox Code Playgroud)

因此,如果您的服务器中没有匹配的路径,则会提供index.html.然后javascript将执行并且react-router(客户端路由)将接管并显示路由的正确组件.

对于大多数SPA来说都是如此,其中路由发生在客户端.