Laravel 8部署Apache 2.4无法读取.htaccess功能按钮点击返回404求助
核心问题说明
你当前的404错误是因为Apache站点根目录指向了Laravel项目根目录,而非Laravel规范要求的public目录,路由重写规则无法正常生效导致的,同时该配置还存在.env敏感文件泄露的安全风险。
解决方案
1. 修改Apache站点配置
分别编辑80端口配置文件/etc/apache2/sites-available/admin.example.com.conf和443端口SSL配置文件/etc/apache2/sites-available/admin.example.com-le-ssl.conf,修改两处核心配置:
- 将
DocumentRoot参数的值从/var/www/html/admin.example.com改为/var/www/html/admin.example.com/public - 将
<Directory>节点的路径从/var/www/html/admin.example.com/改为/var/www/html/admin.example.com/public/(443配置默认没有Directory节点,需要手动补充)
修改后的SSL配置参考:
<IfModule mod_ssl.c> <VirtualHost *:443> ServerName admin.example.com ServerAlias admin.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/admin.example.com/public <Directory /var/www/html/admin.example.com/public/ > Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLCertificateFile /etc/letsencrypt/live/admin.example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/admin.example.com/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf </VirtualHost> </IfModule>
2. 清理冗余配置
删除Laravel项目根目录下的.htaccess文件,仅保留public目录下的.htaccess即可。
3. 重启Apache生效配置
执行命令:
sudo systemctl restart apache2
4. 额外排查项
如果修改后仍然存在404,按顺序检查:
- 给项目必要目录设置正确权限:
sudo chown -R www-data:www-data /var/www/html/admin.example.com/storage /var/www/html/admin.example.com/bootstrap/cache sudo chmod -R 755 /var/www/html/admin.example.com/storage
- 清理Laravel缓存,到项目根目录下执行:
php artisan config:clear php artisan route:clear php artisan cache:clear
- 检查路由定义:确认Start按钮触发的请求路径,和
routes/web.php或routes/api.php中定义的路由一致,如果是接口请求需要确认路径是否带有/api前缀。
内容的提问来源于stack exchange,提问作者Ranger
相关产品推荐
相关产品推荐

