Introduction to .htaccess Files
- On This Page
- About .htaccess Files
- Creating .htaccess Files
This article provides an overview of .htaccess files. It explains what they are and how to create them. This article does not cover individual server directives used within .htaccess files.
You can control the way the Web server responds to requests to your files by creating a .htaccess file in your Web directory or one of its subdirectories. A .htaccess file contains one or more server directives--small commands known by the server--that tell the server how to behave. For example, you can turn on password protection by using a .htaccess file.
Here are some specific properties to know about .htaccess files:
- A .htaccess file must be a plain text file.
- A .htaccess file contains server directives, one per line.
- A .htaccess file applies server directives to a directory as well as its subdirectories.
- A .htaccess file in a subdirectory overrides any .htaccess files found in its parent directories.
- Each line in a .htaccess file that starts with a # symbol is ignored.
Use a text editor such as Pico, Notepad, or SimpleText to create and modify .htaccess files. These editors specialize in plain text files.
If you want an easy way to create .htaccess files, try Pico. For example, to create a .htaccess file in your top-level Web directory:
At the prompt, enter the following command to change directories to your Web directory:
At the prompt, enter the following command to create a .htaccess file in the current working directory:
Use Pico to add the directives you want to your .htaccess file.
Save and close the file using Pico. (Click for instructions on how to use pico).
You can find more instructions on how to use pico, as well as screenshots, here.
If you need help with Unix commands like these, see Getting Started With Unix.
To create a .htaccess file on your desktop computer use Notepad (Windows) or SimpleText (Macintosh). If you use a word processing program to create your .htaccess files be sure to save them as ASCII or plain text.
When you move a .htaccess file from your desktop computer to your Web directory remember to transfer it in text mode. Again, this helps preserve the plain text characteristic so important to .htaccess files.
Here is a one line .htaccess file used to redirect "404 Not Found" responses to a specific Web page:
ErrorDocument 404 /webdemo/404.html
Here is a more elaborate .htaccess file that associates .dot files to a specific MIME type and turns on basic password protection:
AddType application/vnd.msword dot
AuthName "Town of Bedrock"
require user fred