Prevent Directory Listing in WordPress

A question I see pop up from time to time in the WordPress Support Forum is how do I stop people from viewing my plugins folder? There are several ways to go about this - all of them easy.

The first method involves placing an empty index.HTML file in EACH folder you want to protect. If you prefer you can include a brief message in that file rather than using a blank one. Two important things to remember when using this method:

  1. You'll have to place the file in EACH folder you want to protect. It will not shield subfolders.
  2. Be sure NOT to use a blank PHP file in the plugin directory - it will bork the dashboard. That is why I suggest using a blank HTML file. And if you're going to use a HTML file in the plugin directory you may as well use them everywhere else.

The next two methods involve the use of a .htaccess file.

Method a: This is the easiest method of all. Add the following line to the .htaccess file that lives at root...

Options -Indexes

This will automagically turn off indexing for ALL folders/subfolders sitewide. If you add that line to a .htaccess file in wp-contents it will disable indexing not only for that folder but for the folders below it and so forth.

Method b: Add the following line to the .htaccess file that lives at root...

IndexIgnore *

The * matches all files in the directory. What is the difference between the two? Method b allows you to restrict only a subset of files from being viewed. For example, let's say for some reason you want the directory content to be viewable but block image files. You would do this...

IndexIgnore *.gif *.png *.jpg

For more .htaccess goodness check out Stupid htaccess Tricks by Perishable Press.

About Len Kutchma

Len has been blogging for over 10 years and is a rabid WordPress fan. In addition to blogging here you can find him writing the occasional article and toiling away in the forums at WeblogToolsCollection.com. He also hangs out at the WordPress support forums lending a hand where he can. Be sure to follow @wpcanada on Twitter.

Comments

  1. I usually use the index.html in all folders, dont forget image and css folders as well.

  2. Len Kutchma says:

    "...dont forget image and css folders as well."

    Yes. That's why I said to place it in each folder - it will not shield subfolders.

Please Note: WPCanada is a moderated community. Please read the Comment Policy.

HAVE SOMETHING TO SAY?

*