您好,我需要部署React应用。
为了实现这一点,我运行:“ 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>
我还添加到etc / hosts hello.local
当然我已经在httpd.conf中启用了mod重写
当我运行我的react应用程序的hello.local /主页时,显示正确,但是当我想进行react-react路由路径hello.local / example时,我收到404 not found错误。请帮忙可以吗?Apache配置问题或react-router有一些错误?问候
这是SPA的常见问题。在SPA中,路由主要发生在客户端。在您的情况下,大多数情况下react- router应该做好这项工作。由于整个js捆绑为一个文件并在中提供index.html,因此您需要为服务器中的index.html所有路径提供non- existing服务。
react- router
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]
因此,如果服务器中没有匹配的路径,则将提供index.html。然后,javascript将执行,并且react- router(客户端路由)将接管并显示该路由的正确组件。
对于大多数SPA来说都是如此,其中路由发生在客户端。