Apache + PHP 環境,htdocs 目錄下有 `index.html` 和 `test.php`。
訪問 `https://localhost` 正常顯示 `index.html`。
重命名 `index.html` → `index11.html` 後,再訪問 `https://localhost` 報錯:
403 Forbidden
You don't have permission to access / on this server.
預期應顯示目錄列表(`index11.html` 和 `test.php` 鏈接),但實際被禁止訪問。
Apache 默認配置:
當請求目錄 `/` 且無匹配的 DirectoryIndex 文件時,若 `Indexes` 關閉,直接返回 403。
編輯 `httpd.conf` 或站點配置文件:
<IfModule dir_module>
DirectoryIndex index.html index.php index11.html
</IfModule>
重啟 Apache。此後訪問 `https://localhost` 會自動查找 `index11.html`。
優點:無安全風險,符合生產環境規範。
缺點:需修改配置文件並重啟服務。
<Directory "D:/APMServ/www">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
重啟 Apache。訪問目錄無默認文件時,將顯示文件列表。
⚠️ 警告:生產環境嚴禁開啟,會暴露服務器目錄結構、源代碼、配置文件等敏感信息。
瀏覽器輸入:https://localhost/index11.html 或 https://localhost/test.php
優點:零配置修改,立即生效。
缺點:不適合作為網站入口,用戶體驗差。
DirectoryIndex index11.html index.php index.html
# Options -Indexes # 確保關閉目錄瀏覽
# 檢查語法
apachectl -t # Linux/Mac
httpd -t # Windows
# 重啟服務
systemctl restart httpd # CentOS 7+/RHEL
service apache2 restart # Ubuntu/Debian
# Windows: 服務管理器或 httpd -k restart