
本文详解如何修改 XAMPP 内置 Apache 的 DocumentRoot 和对应目录权限,使访问 http://localhost/ 时直接显示指定目录(如 dashboard)下的文件列表,而非重定向到 /dashboard/ 子路径。
本文详解如何修改 xampp 内置 apache 的 documentroot 和对应目录权限,使访问 `http://localhost/` 时直接显示指定目录(如 `dashboard`)下的文件列表,而非重定向到 `/dashboard/` 子路径。
在 XAMPP 环境中,Apache 默认将 http://localhost/ 指向 htdocs 目录(即 C:
mpphtdocs 或 /opt/lampp/htdocs),但 XAMPP 自带的首页实际通过 index.php 重定向至 /dashboard/。若你希望直接以某个子目录(例如 htdocs/dashboard)作为网站根目录,并启用目录索引(即自动列出文件与子目录),需同时调整两个核心配置:DocumentRoot(文档根路径) 和 对应的
✅ 正确配置步骤(Windows/macOS/Linux 通用)
-
定位 Apache 配置文件
打开 XAMPP 安装目录下的主配置文件:- Windows: C: mpppacheconfhttpd.conf
- macOS/Linux: /Applications/XAMPP/xamppfiles/etc/httpd.conf 或 /opt/lampp/etc/httpd.conf
-
修改 DocumentRoot
查找并修改以下行(通常位于文件中部):DocumentRoot "C:/xampp/htdocs"
改为指向你的目标目录(例如 dashboard):
DocumentRoot "C:/xampp/htdocs/dashboard"
-
同步更新
权限配置
向下查找紧邻的块(路径需与 DocumentRoot 严格一致): <Directory "C:/xampp/htdocs">
修改为:
<Directory "C:/xampp/htdocs/dashboard"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>⚠️ 关键说明:Options Indexes 是启用目录列表显示的必要指令;FollowSymLinks 允许符号链接;Require all granted(Apache 2.4+)替代旧版 Allow from all,确保访问权限开放。
禁用 dashboard 重定向(可选但推荐)
若仍跳转到 /dashboard/,检查 C: mpphtdocsindex.php(或对应路径)是否包含类似 header('Location: /dashboard/'); 的跳转逻辑。建议临时重命名该文件(如 index.php.bak),避免干扰根目录行为。-
重启 Apache 生效
通过 XAMPP Control Panel 点击 Stop → Start Apache,或命令行执行(Linux/macOS):sudo /opt/lampp/lampp restart
? 验证与故障排查
- 访问 http://localhost/,应直接列出 dashboard/ 目录下的所有文件(前提是该目录非空且无 index.html/index.php 等默认索引文件——否则 Apache 会优先加载它们)。
- 若提示 403 Forbidden:检查
路径拼写是否完全匹配 DocumentRoot,且 Require all granted 已正确配置。 - 若仍显示空白页或 404:确认目标目录存在、路径使用正斜杠 / 或双反斜杠 \(Windows),且无隐藏的 .htaccess 限制。
? 总结
修改 Apache 默认路径的核心在于保持 DocumentRoot 与










