用webpack打包的前端代码只有1个index.html实体文件,react-router控制路由。
比如:localhost/index.htmlapache访问的是入口文件:index.html.
但是访问localhost/list,路由由react-router控制,但是apache却会查找项目目录下的list.html文件。
apache找不到该文件,报404错误。
解决的方法,(我找到的是)开启rewrite功能,并写个.htaccess,让所有404的页面都rewrite到index.html
主要步骤:
1: 开启apache rewrite功能:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
2: 配置apache conf:
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Order deny,allow
Allow from all
AllowOverride All
3:在项目根目录下写个.htaccess文件:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ / [L,QSA]
但是我在浏览器端输入localhost/list依旧报404错误。求高手指点,是我找我了方法,还是方法细节有bug。
thx。
当使用Webpack打包的React应用通过Apache服务器运行时,遇到404错误,因为Apache尝试加载不存在的list.html。解决方法是启用Apache的rewrite模块,并在项目根目录下创建.htaccess文件,将所有404请求重定向到index.html。确保Apache配置允许Override,并在.htaccess中设置适当的重写规则。然而,按照提供的配置,问题仍然存在。可能需要检查Apache配置和.htaccess文件的正确性。
948

被折叠的 条评论
为什么被折叠?



