Learning Movable Type: What is .htaccess?

htaccess files can give you extra control over your server, allowing you to password protect directories, enable server side includes, generate custom error messages, and block users by IP address among other things. (See this Guide to .htaccess for more information.)

An htaccess file is a simple ascii text file which you create or edit in a text editor. The dot (.) before the word htaccess indicates that .htaccess is a file extension. Some text editors will add on an additional .txt file extension when you save your document. When you upload it to your server however, make sure the final file is named .htaccess and set the file's permissions to 644. The (.) in .htaccess also makes the file "hidden" on a unix server. Some FTP clients do not show the .htaccess because of its hidden status. For such FTP clients you will need to specify the "-a" parameter which displays all files including those that are hidden. For example:


Some web hosts do not allow use of .htaccess files, so check with your web host to make sure that it does before attempting to upload one. According to this Guide to .htaccess, "Most commands in htaccess are meant to be placed on one line only, so if you use a text editor that uses word-wrap, make sure it is disabled or it might throw in a few characters that annoy Apache to no end, although Apache is typically very forgiving of malformed content in an htaccess file."

The .htaccess file usually resides in the root public_html directory of your server. When placed in the root public_html directory, the .htaccess file contents affect every file in your public directory, including every file in every sub directory. For example public_html/.htaccess would affect files in public_html as well as files in public_html/files/ and public_html/files/morefiles. You can override the effects of the root public_html .htaccess file in a subdirectory by placing another .htaccess file in that subdirectory.

Note: You should really know what you are doing before attempting to work on an htaccess file. It is a powerful and potentially dangerous file. In some extreme cases you could lock yourself out of your domain completely, including the use of back-end tools such as cPanel and FTP. (I once took my site offline for several hours unintentionally after changing the code on a .htaccess file. Fortunately my web host tech support came up with the proper code that got the file and the site working again.) If you are unsure, check your .htaccess code with your web host before uploading. I recommend experimenting with a .htaccess file in a subdirectory first, to avoid causing site-wide problems.

Comprehensive Guide to .htaccess
Behind the Scenes with Apache's .htaccess

This tutorial was extracted from a much longer one - Htaccess and Dynamic Publishing- co-authored by Elise Bauer and Arvind Satyanarayan.

Posted by Elise Bauer on September 18, 2004 8:03 PM to Learning Movable Type http://www.learningmovabletype.com/