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

.htaccess配置问题:访问根路由时打开index.html而非index.php

问题:访问网站根路由时自动打开index.html而非index.php

现有配置

.htaccess 文件内容

DirectoryIndex index.php

<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>

Laravel 路由配置

Route::get('/', function () {
    return view('index',[
        'bg' => 'bg-nayapink',
        'active' =>'Home'
    ]);
});

Route::get('/services', function () {
    return view('services', [
        'active' => 'Services',
        'bg' => 'bg-nayayellow'
    ]);
}); 

问题现象

部署在共享服务器上的网站,访问根域名(mysite.com,对应路由/)时,总是加载index.html文件,而非通过Laravel的根路由加载index.php;其他路由(如/services)均可正常访问。

解决方案

方法1:调整索引文件优先级

共享服务器通常默认将index.html设为优先加载的索引文件,即使指定了DirectoryIndex index.php也可能被全局配置覆盖。修改.htaccess的索引配置,明确将index.php放在最前面:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

替换原有的DirectoryIndex index.php行即可,服务器会优先查找并加载index.php,仅当index.php不存在时才会加载index.html。

方法2:强制根路由指向index.php

如果方法1无效,可在.htaccess的RewriteEngine On之后添加一条专门针对根路由的重写规则,强制访问/时直接加载index.php:

# Force root request to load index.php
RewriteRule ^$ index.php [L]

这条规则会拦截根路径请求,直接转发到index.php,绕过服务器的默认索引文件判断。

方法3:删除多余的index.html文件

如果根目录下的index.html是测试遗留或无用文件,直接删除该文件,服务器就会自动加载index.php,这是最直接的解决方式。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 02:03:22