How to stop indexing of files in a directory using .htaccess?
Sometimes we do not want to show all the files in a directory (which by default is a behavior of web servers).
That can be done using htaccess file.
Though there’s a simple solution to this, put an index.html file showing a decent message “Not authorized to access this”. But that will have little control over management.
What if we want to have more control and do it at the initial stage only?
Use .htaccess file.
Put this in your .htaccess file
here * (asterisk) matches all the files so none would be displayed.
If you do not want to show .css or .conf or .php or .sh files only and let other files being shown, then,
IndexIgnore *.css IndexIgnore *.php IndexIgnore *.sh IndexIgnore *.conf
or in a single line
IndexIgnore *.css *.php *.conf *.sh
This can be made to work for relative paths also, if you want to put all your htaccess statements in a single file at root level (recommended)
To make some file like “myfile.html” your default file (instead of index.html) which opens when a user enters a URL to access some directory, then use,
You can specify more than one file names, in case first is not found then immediate next one would be opened.
DirectoryIndex myfile_1.html myfile_2.html
On the contrary, if you want to show file index under a directory (opposite of stopping index) then use this,
Secure your files from accidental display to the users,
<Files ".htaccess"> Order Deny,Allow Deny from all Allow from 126.96.36.199 </Files>
Use regular expressions (using ~) to specify file names (to secure files other than .htaccess)
<Files ~ "\.(gif|jpe?g|png)$"> Order Deny,Allow Deny from all Allow from 188.8.131.52 </Files>