Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-load index.html in sub-directories with Apache 2

Tags:

apache2

I'm having problem auto-loading index.html in sub-directories with Apache 2. Auto-loading of index.html works fine for root directory.

You see, to make things generic, my web app is written such that each module resides in its own sub-directory. Each folder has 3 files - index.html for the front end (contains html + javascript), index.php for the backend (contains php code to access database) and index.css for styling.

Hence, to access the various modules in the web app:

[Overview module] - http://xyz.com/overview?id=1234567890

[Details module] - http://xyz.com/details?id=1234567890

Without the auto-load mechanism for sub-directories, the above would not be possible.

I would appreciate any help. Thanks much!

like image 742
glendon Avatar asked Jun 20 '11 02:06

glendon


2 Answers

Finally resolved it with a colleague.

The default DirectoryIndex specified in httpd.conf didn't work for us. Even though our sequence is 'index.html' then 'index.php', Apache2 will serve out 'index.php' first. Only when 'index.php' is not present in the same folder, then 'index.html' is served out.

We found 2 ways to overcome that:

Assuming your doc root is '/var/www/html',

[Method 1]
1.  Add a .htaccess to the root directory of your web app (e.g. /var/www/html/myapp).
2.  Add the line 'DirectoryIndex index.html' to the .htaccess.
3.  In httpd.conf, set 'AllowOverride' to 'All' under <Directory '/var/www/html'>.
[Method 2]
1.  In httpd.conf, add 'DirectoryIndex index.html' under <Directory 'var/www/html'>.
(note: this 'DirectoryIndex' is different from the default DirectoryIndex that is 
not enclosed within any tag.)

Restart the web server.

Hope this can help someone. Thanks!

like image 184
glendon Avatar answered Nov 11 '22 11:11

glendon


You can use the following line in your .htaccess file:

DirectoryIndex index.html

Works even if you don't control the server configuration (if you use Gitlab Pages for example).

like image 21
roneo.org Avatar answered Nov 11 '22 10:11

roneo.org