Apache

Guide – Usage of DirectoryIndex in Apache/2 with (.htaccess) File

DirectoryIndex used to allow you to land default page when client access a directory. For example, When a visitor request a directory to access it on your website, you can define the file to load when they request to a directory. To display a “index.html” file instead of “index.php” when client request a directory . Simply we can create a .htaccess file on our root directory or any other location.

Setup index.html as a default page when directory accessed

#vim /var/www/html/.htaccess

<Directory /var/www/html/web>
DirectoryIndex index.html
</Directory>

Here we have define default page “index.html” to load if client request  “/web” directory.

We can also define multiple file to load default if client access “/web” directory.

Setup multiple file as a default page when directory accessed

#vim /var/www/html/.htaccess

<Directory /var/www/html/web>
DirectoryIndex index.html index.php index.shtml
</Directory>

You can see I have define DirectoyIndex first load default page “index.php” than “index.php” and so on.

If client request a directory web server try to load first “index.html” file. If “index.html” file is not available on the accessed directory, than web server try to load next define file “index.php” and so on.

If non of them files are available on directory. Apache server try to revert it’s default setting  and it will display an error message like “ 403 Error-Forbidden!” . If you get this error that means Indexing is not allowed on your web server.

Options  -Indexes

Here you can see my vhost configuration file for example.

<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
CustomLog /var/log/httpd/vhosts/www.example.com/access_log combined
ErrorLog /var/log/httpd/vhosts/www.example.com/error_log

<Directory /var/www/html>
Options -Indexes -MultiViews +FollowSymLinks
#AllowOverride None
AllowOverride All
#AllowOverride AuthConfig Limit FileInfo Order allow,deny Allow from all

<Directory /var/www/html>
DirectoryIndex index.html
</Directory>

<Directory /var/www/html/web>
DirectoryIndex index.html index.php index.shtml
</Directory>

<Files ~ "^\.ht">
Deny from all
</Files>
</Directory>

</VirtualHost>

After editing your Apache configuration file you will need to restart/reload Apache service.

# service httpd reload
OR
# service httpd restart

That’s it.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment