An Introduction to the .htaccess File submitted by Steve Thompson


Increase Flexibility With Distributed Configuration Files
by Steve Thompson


The .htaccess file, or "distributed configuration file," allows Webmasters to configure changes on a per-directory basis. This simple ASCII file contains several lines of configurations that, when placed in a particular directory, apply to all sub-directories. This file gives Web publishers who do not have root server access an alternative to make specific server changes.

The odd naming convention ".htaccess" denotes a file extension name for a file with no name.


Configurations

There are many powerful settings you can change on your server using .htaccess files. Creating the .htaccess file is simple. Open a text editor and save the new document as ".htaccess" (notice the quotations) in a test directory on your Web server. The quotation marks are necessary when saving the file to ensure that no extension is added to the file. Remember you can create as many .htaccess files as you want. Each one can have its own configurations specific for each directory.

You must upload .htaccess files in ASCII mode, not binary. You may need to CHMOD the .htaccess file to 644 or (RW-R--R--). This makes the file usable by the server, but prevents it from being read by a browser (which can seriously compromise your security). In most cases, your host will allow .htaccess file support because it saves them a lot of work.


Custom Error Pages

Instead of having the default 404 Page Not Found, you can customize your own error message with a few lines of code in your .htaccess file. You can use custom error pages for any error as long as you know its number.

Example: <ErrorDocument 404 /notfound.html> <ErrorDocument 401 /authorization-required.html> <ErrorDocument 403 /forbidden.html> <ErrorDocument 500 /internal-server-error.html> Just create the files to which the ErrorDocument function points. There are several other types of errors; these are just a few of the most common.


Password Protection

One of the most common uses of the .htaccess file is protection of a specific directory. This is done by requiring the viewer to type in a user name and password to view the document. First, put the following lines of code in the .htaccess file:

Example: <AuthName "Section Name"> <AuthType Basic> <AuthUserFile /full/path/to/.htpasswd> <Require valid-user> Next, create a file called ".htpasswd" and add just one line of code: <username:password> You can encrypt the password by searching online for pre-made scripts.


Redirects

The .htaccess file enables you to point old links to new locations.

Example: <Redirect /directory/page1.html http://www.yourdomain.com/directory/page2.html> It is good practice to place the .htaccess file in the topmost directory possible, so that all sub-directories that may have the link can recognize the configuration.

There are several other configurations that are allowed in the .htaccess file including:

• Allow/deny IP addresses
• Parse PHP in .html files
• Allow SSI in .html files
• Add new MIME types
• Rewriting the URL

The power of this small file can be large. It can present opportunities to build a more complex Web site and increase security.



ArgentosOnline is © 2006, Cesar Cainelli.
All design, unless stated otherwise, is by Cesar Cainelli.
Argentos Online staff can be reached at webmaster@argentosonline.com.