You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel部署后API路由404错误求助:恢复index.php仍无效

解决Laravel项目除index.php外全404的问题

检查Web服务器重写配置

  • Apache环境:
    确认项目public目录下的.htaccess是Laravel默认配置,内容如下:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # Send Requests To Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

    同时确保服务器已启用mod_rewrite模块,执行a2enmod rewrite启用后重启Apache。

  • Nginx环境:
    检查站点配置文件的location /规则,需包含正确的try_files配置:

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    

    修改后重启Nginx服务。

修复文件权限与所有者

确保Laravel核心目录的读写权限正确:

chmod -R 755 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache # 需根据服务器运行用户调整,比如部分环境用nginx用户

重置Laravel缓存

替换文件可能导致缓存异常,执行以下命令清理并重建缓存:

php artisan config:clear
php artisan route:clear
php artisan cache:clear
php artisan view:clear
# 生产环境可执行以下命令重新生成缓存
php artisan config:cache
php artisan route:cache

确认文档根目录配置

检查服务器的文档根目录是否指向Laravel项目的public文件夹,而非项目根目录。这是Laravel部署的标准要求,若之前替换index.php时修改过路径,需改回public目录。

排查ErrorDocument配置

针对错误提示中"使用ErrorDocument处理请求时遭遇404"的问题,检查服务器配置中的ErrorDocument设置,确认自定义404页面路径正确。可暂时注释该配置,测试是否解决问题后再重新配置。

内容的提问来源于stack exchange,提问作者adham allan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:43:15