Apache虚拟主机配置异常:访问跳转至目录列表而非首页
Apache虚拟主机跳目录列表问题排查&解决
问题根源
访问website.test跳转到目录列表,本质是Apache没找到你指定的默认索引文件——要么是没配置索引规则,要么是关键模块未启用,还有可能是站点路径配置错误。
具体排查&解决步骤
1. 确认mod_dir模块已启用
打开httpd.conf,找到以下配置行,确保没有被#注释:
LoadModule dir_module modules/mod_dir.so
如果前面有#,删除注释后重启Apache即可。
2. 明确指定默认索引文件
可以在httpd.conf全局配置中添加,也可以在对应虚拟主机块内单独配置(推荐后者,仅影响当前站点):
<VirtualHost *:80> ServerName website.test # 注意:路径必须指向public-html子文件夹,而非上级website目录 DocumentRoot "/你的实际路径/Sites/website/public-html" # 核心配置:告诉Apache优先查找的索引文件顺序 DirectoryIndex index.php index.html <Directory "/你的实际路径/Sites/website/public-html"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
DirectoryIndex的顺序代表优先级,先找index.php,找不到再找index.html,可按需调整。
3. 检查DocumentRoot路径正确性
务必确认DocumentRoot指向的是public-html文件夹,而非上级的website目录。如果路径错误,Apache找不到目标索引文件,就会默认展示目录列表。
另外,要确保Apache对该目录有读取权限,Linux/macOS下可执行以下命令调整:
sudo chmod -R 755 /你的实际路径/Sites/website/public-html
4. 关闭目录列表功能(可选)
如果不需要Apache展示目录内容,可修改<Directory>块中的Options项,去掉Indexes:
<Directory "/你的实际路径/Sites/website/public-html"> Options FollowSymLinks AllowOverride All Require all granted </Directory>
这样当没有找到索引文件时,Apache会返回403禁止访问,而非目录列表。
验证操作
修改完配置后,重启Apache服务(macOS用sudo apachectl restart,Linux用sudo systemctl restart httpd),清除浏览器缓存后重新访问website.test,即可验证是否正常加载索引文件。
内容的提问来源于stack exchange,提问作者SK111
相关产品推荐
相关产品推荐

