Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Usability

Web Design Reference Guide

Hosted by


Last updated Oct 17, 2003.


.htaccess (a period appears before the name) is the little engine that could in the host directory. With this simple text file, you can make configuration changes to the server or change the directives (instructions) for a directory and its sub-directories. Those custom 404 pages? .htaccess is responsible.

.htaccess is located in the directory where you want the changes to occur. An .htaccess file located on the main directory affects everything on your Web site. If you want to change the directives for one directory and not the whole site, that directory gets its own .htaccess. .htaccess files in specific directories get priority over the ones "above."

Let's say we have a public_html directory, which contains the whole site, and a blog directory. .htaccess exists in both directories, but whenever they have similar commands, the one in the blog directory overrides the file in the public_html directory. It's like cascading style sheets (CSS). The lower you go, the higher it gains in priority.


You might have added directives to an .htaccess file without knowing it. Many Web hosts have a GUI-based control panel. It has options such as Redirects, Error Pages, and Hotlink Protection. Redirects allow you to send visitors to a different Web page. If your page's URL used to be and you move it to you can create a redirect so that when visitors go to the old URL, the server automatically sends them to the new URL. In the control panel, all you have to do is enter the old URL, the new URL, and click Add. This creates a new directive in .htaccess with the following:

redirect /user/blog/index.html
redirect /user/blog/index.php

You can open the .htaccess file just as you would any text file. These two lines appear in the file.

Error Files

Creating your own 404 presents an opportunity to learn about the power of .htaccess. Start by producing a 404 HTML page (can be php or whatever you're using) and save the file. Note the file name (we use 404.html in our example). Add the following line in .htaccess (this assumes 404.html is located at the root directory):

ErrorDocument 404 /404.html

If the file is located in a different directory, then add the path in front of /404.html. For example, when all error files are in the /errors directory, then use:

ErrorDocument 404 /errors/404.html

Create error files for the other error messages. Here's a list of the common error codes:

400 - Bad Request
401 - Authorization Required
403 - Forbidden
404 - Not Found
500 - Internal Server Error
503 - Service Unavailable

When entering the directive into .htaccess, just replace 404 with the error code you want to use (ie. ErrorDocument 400 /errors/404.html).

General Information

Ensure your text editor has word wrap turned off because each command typically goes on one line. Word wrap adds characters that could scare Apache, though it forgives some non-standard formatting.

With .htaccess you can restrict access to certain directories, stop specific IP addresses from accessing your content (if you know someone's IP address and you don't want him going to your Web site or a specific area of your Web site, add the IP address to .htaccess to deny access), use user IDs and passwords, add or override extensions, shorten URLs and much more. The Apache Web site has a section on directives and a how to.

If your server uses Windows, then .htaccess won't work. It's limited to Apache servers (UNIX). You can download Windows Binary of Apache and install it on your computer to play with it. For those still on Windows 95, there are instructions for installing Apache. Running Apache on your PC offers an excellent way to learn how things work behind the scenes of a Web site.

Check with your Web server host to see if you have .htaccess and the ability to edit it. Some hosts only give you access to it through the control panel GUI like cpanel while others let you open and edit it. If you have the ability to create one from scratch, open a text editor and save the new file as .htaccess. If the editor adds .txt to the file name, remove it by renaming it. It's the little file that could do everything.