Folks, I'm trying to grapple with something which is making my head spin. I wonder if someone might have an idea on this.

It's well understood, that a webserver can supply a specified file, by default, if no page is specified on the URL:
eg.

can be requested by the URL
http://www.london.edu/
because index.html is configured as a default page.

I'm trying to discover if Apache offers the feature to specify the default page on a folder-by-folder basis.
eg.


can be requested by the URL
http://www.london.edu/
by configuring index.html as a default page for the root folder.

and also


can be requested by the URL
http://www.london.edu/phd/
by configuring phd.html as a default page for the phd folder.

and also

http://www.london.edu/mba/mba.html
can be requested by the URL
http://www.london.edu/mba/
by configuring mba.html as a default page for the mba folder.

etc.

I'm not sure if I'm reading it right, but this page may have some bearing.
http://httpd.apache.org/docs-2.0/mod/mod_dir.html

Many thanks for any light you can shed on this. I'm certainly in the dark.

Dani AI

Generated

Good work by — that is the right direction. Apache’s mod_dir supports per-directory default files: the DirectoryIndex list is checked in order and the first match is returned, and the directive can be applied in server config, vhost, <Directory> blocks or a .htaccess. Mod_dir also provides options such as external redirects for an index resource. (httpd.apache.org)

A couple of practical notes that fill the gaps from the thread. .htaccess only takes effect if the server’s AllowOverride permits the Indexes (or All) class, and using .htaccess is slower than putting the same directives in the main config — use <Directory> blocks when you control the server. If content negotiation is enabled (MultiViews) Apache may pick a different file than you expect; disable it in that directory for deterministic results. (httpd.apache.org)

Troubleshooting checklist and examples:

  • Make sure the file is named exactly .htaccess (not .httaccess) and is readable by Apache; on Windows save with quotes (e.g., ".htaccess") or create it from the command line. Check file permissions (e.g., 644) and SELinux contexts on Linux. Look at error_log if it’s not being applied.

  • Quick .htaccess pattern to avoid negotiation and set two fallbacks:

    # in a directory's .htaccess
    Options -MultiViews
    DirectoryIndex index.html index.php
  • If you can edit the main config, prefer a <Directory> block instead:

    <Directory "/var/www/html/phd">
        Options -MultiViews
        DirectoryIndex phd-home.html index.html
    </Directory>

For external-redirect behavior or other nuances of index selection see mod_dir’s notes on DirectoryIndex and DirectoryIndexRedirect. (httpd.apache.org)

Recommended Answers

All 3 Replies

You will want to add:

DirectoryIndex

to the .htaccess file for each directory. There may be another way to do this, but this should work.

So for example:

In the directory: phd/
add this line to your .htaccess: DirectoryIndex phd.html

Many thanks. I'll give it a go.

And it worked....

Using Apache on Windows:
1. Install.
2. Modified httpd.conf to change "AllowOverride None" to "AllowOverride All"
3. Created folder called phd within folder htdocs.
4. Put a file in this folder, called phd.html
5. Used command line to create .httaccess in the folder phd.
6. Put one line in the .htaccess file: DirectoryIndex phd.html
7. Restarted the server.

RESULT!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.