A directory URL such as /docs/ can open a landing page without the visitor typing its filename. Apache's DirectoryIndex setting determines which page opens when several index files are available. On Debian and Ubuntu, you can set that order in the configuration for the site that serves the URL.
Apache tries the names from left to right and serves the first match. A directory can use its own order in place of the server default. Keeping the old index file later in the list gives Apache a fallback. If no index file matches, the server can still show a directory listing when Indexes is enabled.
A <Directory> block inside a virtual host applies to a filesystem path on that site, including its subdirectories unless a more specific rule changes the order. The index files need to work when requested by name before they can serve as landing pages. A PHP index, for instance, needs a working PHP handler for the site.
Steps to configure DirectoryIndex in Apache:
- Find the virtual host for your site's listener and hostname from your ordinary user session on the Apache server.
$ sudo apache2ctl -S VirtualHost configuration: *:80 docs.example.com (/etc/apache2/sites-enabled/docs.conf:1) ##### snipped #####
The docs.example.com line shows docs.conf under /etc/apache2/sites-enabled/. On a standard Debian or Ubuntu setup, that file links to the editable copy in /etc/apache2/sites-available/. You need a user account with sudo access for the configuration changes.
- Back up the editable virtual host file, substituting your site's file for docs.conf in both paths.
$ sudo cp -pi "/etc/apache2/sites-available/docs.conf" \ "/etc/apache2/sites-available/docs.conf.bak"If the backup already exists, decline the overwrite and rerun with an unused backup name. If you need to roll back, use that backup name.
- Open the editable virtual host file with
sudoedit, substituting your site's file for docs.conf.$ sudoedit "/etc/apache2/sites-available/docs.conf"
For the site's root URL, use the
DocumentRootin the matched <VirtualHost> block. For http://docs.example.com/, the block setsDocumentRoot /var/www/docs. TheMain DocumentRootfromapache2ctl -Sbelongs to the main server.If your default editor differs from Nano, prefix the command with
SUDO_EDITOR=nanoto use Nano. - Insert the <Directory> block before the matched site's closing </VirtualHost> tag, substituting your directory path for /var/www/docs, your preferred filename for home.html and your fallback filename for index.html.
Stop if a custom order already applies in the Apache configuration or an active .htaccess file.
<Directory "/var/www/docs"> DirectoryIndex home.html index.html </Directory>
- Save the file on exit from Nano.
In Nano,
Ctrl+Xopens the save prompt,Yconfirms the save, andEnteraccepts the filename.sudoeditinstalls the saved file when the editor closes. - Check the Apache configuration syntax.
$ sudo apache2ctl configtest Syntax OK
If the syntax check fails, fix the reported line and rerun the check before reloading. An unknown DirectoryIndex directive can mean mod_dir is not loaded.
- Apply the new index order with a graceful reload of the running Apache server.
Reloading applies every pending Apache edit. Only reload when those changes are ready to go live. If the new order serves the wrong page, restore the backup to the original file path with
sudo cp -p, rerun the syntax check and reload, then request the same URL again.$ sudo apache2ctl graceful
- Request your site's directory URL from your ordinary user session, using the URL with its trailing slash in place of http://docs.example.com/.
$ curl -fsS \ "http://docs.example.com/" <h1>Documentation home</h1>
The response should contain text unique to your preferred page. In the example, Documentation home comes from home.html. The fallback index.html has the heading Welcome. If it returns the old page, check that the URL reaches the virtual host you edited and that no narrower <Directory> rule or active .htaccess file supplies another index order.