Apache 设定了 DirectoryIndex 后, Apache 会自动开启目录下的相应页面, 例如:
DirectoryIndex index.html index.htm index.php
如果没有 DirectoryIndex 设定的页面, 会显示 403 Forbidden 或者显示所有档案。
用 YUM 安装好 httpd 后, 如果网页根目录没有 DirectoryIndex, 默认会出现一个 “Testing 123” 的页面, 这个设定方法很实用, 因为可以在 VirtualHost 没有 DirectoryIndex 档案时, 自动显示默认页面, 例如显示网页正在建构中, 或者提供需要上传 index.html 档案。
这个做法的原理是, 先用 Alias 设定别名指向默认网页档案, 同时在 DirectoryIndex 加多一个不会与其他网页相同的名称, 以下是具体设定方法。
首先建立一个默认页面, 例如 /var/www/default/default_page.html
# vi /var/www/default/default_page.html
将想显示的默认内容放到以上网页档案。
然后开启 httpd.conf:
# vi /etc/httpd/conf/httpd.conf
加入以下一行:
Alias /.default_page.html /var/www/default/default_page.html
然后找到 DirectoryIndex 的一行, 在 DirectoryIndex 最后加上 .default_page.html, 改成类似这样:
|
1 2 3 |
<IfModule dir_module> DirectoryIndex index.html index.htm index.php .default_page.html </IfModule> |
储存盘案后重新启动 httpd 即可:
# systemctl reload httpd
设定后, 所有 VirtualHost 如果的根目录, 如果没有 DirectoryIndex 的档案, 会自动显示 /var/www/default/default_page.html 的内容。