Using .htaccess

by Walt Stoneburner

The Apache Server has the ability to alter it's behavior on a per-directory basis. Rather than specifying explict entries in the httpd.conf file, one can instead make entries in a .htaccess in the directory being served.

Before you go and start overriding Aapache's behavior, you must explictly allow overrides in the httpd.conf file.

The most agressive context is (httpd.conf):
<Directory some_directory>
  Options All
  XBitHack on
  AllowOverride All
</Directory>

Technically, you can also add MultiViews to the Options line, though if you're not dealing with multiple languages it can cause other problems (like serving up pages you don't expect). The XBitHack allows pages with their executable bit set to be processed as a server-parsed html document.

This represents the more common uses of .htaccess.

Prevent Serving Pages From This Directory

You may be asking why you'd want to do that. Suppose you have a set of PHP scripts which need to include data or PHP fragments. Instead of shuffling it outside of the document tree, which is never a bad idea from a web perspective, you can instead add this to the .htaccess file:

order deny,allow
deny from all

Serve a Different Document on a 404 Error

Want a custom page when the user enters a URL that would normally fail? Do this in your .htaccess file:

ErrorDocument 404 /404.php

This says load the 404.php upon failure.

Want PHP to Load Includes Without Explict Paths

The include statement in PHP can be made to look in various directories. Add this to your .htaccess file:

php_value include_path ".:/usr/lib/php:/usr/local/lib/php:/your/directory"

Here PHP will check the current directory, then two common places for PHP libraries, then a directory you specify (which might be a subdirectory in your web space that you blocked serving access to).

SlingCode Search Results About     Articles     Links     Search Tips  
SEARCH: