How to prevent directory indexing
Posted on 04 Feb 2011 by Alan Burns
When accessing a directory (rather than a page) on your site, a visitor will by default see the index page for that directory. If the directory does not contain an index file, the browser will display a list (or index) of the files in that directory.
For example, browsing to yourdomain.com/images/ would, if there were no index file (such as index.html) display a list of the contents, such as shown in this screenshot:
To prevent this directory listing, you can use an .htaccess file (a topic I’ll cover in a future article). If you’re not yet familiar or comfortable working with htaccess, there is a function in cPanel that will take care of it for you.
In cPanel under the Advanced section, click “Index Manager”. Choose public_html or whatever directory you want to change.
Index Manager allows you to customize the way a directory will be viewed on the web. You can select between a default style, no indexes, or two types of indexing. If you do not wish for people to be able to see the files in your directory, choose “no indexing”.
- Select the directory in which you wish to begin navigating your website’s contents using the pop-up window.
- To navigate Index Manager, click the folder icon next to the directory name.
- Click the name of the directory for which you want to change the indexing style.
- cPanel offers 4 options; select 1 of the following:
- Standard Indexing: Contents appear only as filenames.
- Fancy Indexing: Information about the files, such as the size and time last modified, appear.
- Default System Setting: The default defined by your web host will be used.
- No Indexing: The contents of the directory are not listed; visitors will see a message stating that the contents are “forbidden.”
- Click Save.
With directory listing/indexing disabled, someone visiting a directory without an index file will now see this:
cPanel creates the required .htaccess file and entries for you. Because of the way .htaccess works, your new setting applies to the current directory and all sub-directories. If you want a different setting for a particular sub-directory, just use Index Manager again for that directory and select your desired setting.