.htaccess Block Directory Listing Specifying Home Page and Custom Error Pages

.htaccess is your friend

Using .htaccess to block directory listings, specify a home page and do custom error handling are all explained below.

If you’d like some background on .htaccess or see examples of the rewrite rule:

Block Directory Listing

Decent web hosts have this switched on by default, but in case they don’t it’s a good idea to stop users from being able to list the contents of a directory on your web server.

Here’s how to stop it:

Options -Indexes

Yep, that easy. I tend to put this line near the top of the .htaccess file.

 

Specify Home Page

I think this is the default home page list that Apache uses:

  • index.html
  • index.shtml
  • index.php
  • index.htm
  • default.htm
  • Default.htm
  • default.html
  • Default.html
  • default.shtml
  • Default.shtml
  • page1.html
  • index.pl
  • index.cgi
  • index.php3
  • index.phtml
  • home.htm
  • home.html
  • home.shtml
  • index.wml

You can override this using .htaccess with this line:

DirectoryIndex index.php index.html

In this example I have placed index.php first and index.html second. You can have as many as you want, just use a space between page names. Again I usually put this line near the top of the .htaccess file.

 

404 Begone!

Yeah, no-one likes getting a harsh 404 page not found error page, so you can create a nice soft landing for your customers with links and searches, smiles and rainbows:

ErrorDocument 404 /happyerrorpage.html

Again, near top of the .htaccess file.

 

Example .htaccess file

Might look like this:

# -- Index Section --
Options -Indexes
DirectoryIndex index.php index.html

# -- Error Page Redirect --
ErrorDocument 404 /happyerrorpage.html

The # makes that line a comment. Comments are good. Use comments.

Enjoy!

 

Leave a comment