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

Laravel 8.52路由需加index.php才能访问的问题求助

问题:Laravel后台路由无index.php时返回404

环境信息:

  • Laravel版本:8.52
  • Web服务器:Apache 2.4.41
  • 网站根目录:/var/www/new.com/html,其中core文件夹存放Laravel应用,根目录的index.php为Laravel入口文件

当前问题:访问主域名https://www.new.com正常,但访问Laravel后台路由https://www.new.com/admin时返回404错误,必须手动添加index.php(即https://www.new.com/index.php/admin)才能正常访问。

已尝试操作:

  • 在网站根目录配置.htaccess但无效
  • 移除虚拟主机配置改用/var/www/html作为根目录
  • 仅使用80端口
    以上操作均未解决问题,怀疑问题出在Laravel端而非Apache。

相关目录结构

网站根目录(/var/www/new.com/html)

drwxrwxr-x  8 www-data www-data   4096 Feb  2 19:24 assets
drwxrwxr-x 12 www-data www-data   4096 Feb  7 15:39 core
-rwxrwxr-x  1 www-data www-data   1739 Feb  2 19:25 index.php

Laravel应用目录(/var/www/new.com/html/core)

total 960
drwxrwxr-x 12 www-data www-data   4096 Feb  7 15:39 .
drwxrwxr-x  6 www-data www-data   4096 Feb  7 20:08 ..
drwxrwxr-x 10 www-data www-data   4096 Feb  2 19:24 app
-rwxrwxr-x  1 www-data www-data   1686 Feb  2 19:24 artisan
drwxrwxr-x  3 www-data www-data   4096 Feb  2 19:24 bootstrap
-rwxrwxr-x  1 www-data www-data   2335 Feb  2 19:24 composer.json
-rwxrwxr-x  1 www-data www-data 355511 Feb  2 19:24 composer.lock
drwxrwxr-x  2 www-data www-data   4096 Feb  2 19:24 config
drwxrwxr-x  5 www-data www-data   4096 Feb  2 19:24 database
-rwxrwxr-x  1 www-data www-data    220 Feb  2 19:45 .editorconfig
-rwxrwxr-x  1 www-data www-data    977 Feb  6 23:51 .env
-rwxrwxr-x  1 www-data www-data    111 Feb  2 19:24 .gitattributes
-rwxrwxr-x  1 www-data www-data    191 Feb  2 19:24 .gitignore
-rwxrwxr-x  1 www-data www-data    707 Feb  7 00:07 .htaccess
-rwxrwxr-x  1 www-data www-data    730 Feb  2 19:24 package.json
-rwxrwxr-x  1 www-data www-data 521192 Feb  2 19:24 package-lock.json
-rwxrwxr-x  1 www-data www-data   1202 Feb  2 19:24 phpunit.xml
drwxrwxr-x  2 www-data www-data   4096 Feb  6 23:12 public
-rwxrwxr-x  1 www-data www-data   3780 Feb  2 19:24 README.md
drwxrwxr-x  4 www-data www-data   4096 Feb  2 19:24 resources
drwxrwxr-x  2 www-data www-data   4096 Feb  2 19:24 routes
-rwxrwxr-x  1 www-data www-data    563 Feb  7 15:39 server.php
drwxrwxr-x  6 www-data www-data   4096 Feb  2 19:24 storage
-rwxrwxr-x  1 www-data www-data    181 Feb  2 19:24 .styleci.yml
drwxrwxr-x  4 www-data www-data   4096 Feb  2 19:24 tests
drwxrwxr-x 49 www-data www-data   4096 Feb  2 19:25 vendor
-rwxrwxr-x  1 www-data www-data    549 Feb  2 19:24 webpack.mix.js

Apache虚拟主机配置

<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ServerAdmin webmaster@new.com
                ServerName  new.com
                ServerAlias www.new.com
                DocumentRoot /var/www/new.com/html
                DirectoryIndex index.php

                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                SSLEngine on

                SSLCertificateFile      /etc/ssl/certs/www.new.com.crt
                SSLCertificateKeyFile /etc/ssl/private/www.new.com.key

                <Directory "/var/www/new.com/html">
                        Options FollowSymLinks MultiViews
                        Order Allow,Deny
                        Allow from all
                </Directory>


                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>
        </VirtualHost>
</IfModule>

根目录index.php代码

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is maintenance / demo mode via the "down" command we
| will require this file so that any prerendered template can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists(__DIR__.'/core/storage/framework/maintenance.php')) {
    require __DIR__.'/core/storage/framework/maintenance.php';
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/core/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

$app = require_once __DIR__.'/core/bootstrap/app.php';

$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(

解决方案

1. 修复Apache重写规则支持

Apache 2.4+已不再使用旧的权限语法,且需允许重写规则生效:

  • 启用mod_rewrite模块:
sudo a2enmod rewrite
sudo systemctl restart apache2
  • 修改虚拟主机的<Directory>配置块:
<Directory "/var/www/new.com/html">
        Options FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
</Directory>

2. 配置根目录.htaccess规则

在/var/www/new.com/html下创建或修改.htaccess文件,添加以下内容:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # 跳过已存在的文件和目录
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # 将所有请求转发到index.php
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

3. 确认Laravel环境配置

打开core/.env文件,确保APP_URL与域名一致:

APP_URL=https://www.new.com

4. 检查Laravel路由配置

确认core/routes/web.php中存在/admin路由,例如:

Route::get('/admin', [AdminController::class, 'index']);

排查是否有中间件或命名空间限制导致路由无法访问。

5. 清除Laravel缓存

进入Laravel应用目录执行缓存清除命令:

cd /var/www/new.com/html/core
php artisan route:clear
php artisan config:clear

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 21:50:39