apache默认首页由directoryindex指令严格控制,必须显式列出index.php等文件并按优先级排序,配置需置于或等有效块内,修改后须重启服务生效。

Apache 默认只找 index.html,如果目录下只有 index.php 却没配置它,访问根路径就会报 403 或 404。关键不是“让 Apache 自动尝试”,而是**明确告诉它按什么顺序去找**——这个顺序由 DirectoryIndex 指令控制。
确认当前 DirectoryIndex 配置是否包含 index.php
Apache 不会“自动补上”缺失的文件名,必须显式列出。常见错误是配置里只有 index.html 或 index.htm,漏了 index.php。检查方式:
- 打开主配置文件(如
httpd.conf或/etc/apache2/mods-enabled/dir.conf) - 搜索
DirectoryIndex,看是否包含index.php - 若只有
DirectoryIndex index.html,就需修改
修改 DirectoryIndex 指令添加 index.php
把 index.php 加入查找列表,并放在合理位置。例如:
- 想优先加载 PHP 入口:
DirectoryIndex index.php index.html index.htm - 想优先加载 HTML 静态页:
DirectoryIndex index.html index.htm index.php - 多个文件名之间**必须用空格分隔**,不能用逗号、换行或中文标点
- 该指令需写在生效的作用域内:
<virtualhost></virtualhost>、<directory></directory>块中,或全局模块块(如<ifmodule dir_module></ifmodule>)
用 .htaccess 局部覆盖(无需改主配置)
适合没有服务器权限,或只想对某个项目生效的情况:
- 在网站根目录新建或编辑
.htaccess文件 - 写入:
DirectoryIndex index.php index.html - 确保 Apache 已启用
AllowOverride Indexes或All(XAMPP/WAMP 默认开启) -
.htaccess中的设置会**完全替代**全局配置,不合并
修改后必须重启或重载 Apache
配置改完不会自动生效:
- Linux(systemd):
sudo systemctl restart apache2(Debian/Ubuntu)或sudo systemctl restart httpd(CentOS/RHEL) - macOS / 手动安装:
sudo apachectl graceful - Windows(XAMPP/WAMP):通过控制面板点击 “Restart Apache”
- 可先运行
apachectl -t或apache2ctl -t检查语法是否正确
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











