home about/faq features tutorials resources members search

Using the .htaccess File

Collated by Miraz Jordan

The .htaccess file

Web designers often ask how to handle redirects or to password protect directories. The .htaccess file can do these things and more. For this article Miraz Jordan has collated various tips mentioned on the Wisewomen mailing list, and from several other sources.

What the .htaccess file can do

  1. If you're reorganising your site and moving pages around, you can use the .htaccess file to redirect visitors from the old page to the new one.
  2. Another function of the .htaccess file is to allow you to serve up pages which include PHP or Server Side Includes (SSI) but whose file name still uses the .htm or .html extension.
  3. Allow or prevent directory browsing.
  4. Because the server should check the .htaccess file before anything is delivered to the client, you can use it to password protect parts of your site.
  5. You can also block various bots with the .htaccess file — for example, you can keep some spammers out, or prevent search engine spiders from indexing your images folder.

You can read the definitive information on .htaccess files at Apache.org.

Reveal hidden .htaccess files

The filename for the .htaccess file begins with a . (dot). This causes it to be hidden on many Operating Systems. You may have trouble finding or working with such hidden files.

On the server

Set your FTP software to show files beginning with a dot, or access the file through your server's Control Panel — File Manager.

Some FTP software, such as Interarchy, allow you to edit files directly on the server. Select a file and choose Listing menu — Edit with. To get Interarchy to display files whose name begins with a dot visit Preferences — transfers and uncheck Ignore .files.

On Mac OS X

If you download the file to a Mac running OS X you will have trouble finding it as the Mac hides files whose filenames begin with a dot. You can edit hidden files on a Mac though, provided you can find them.

Find invisible files on Mac OS X.

A standard Finder search can find hidden files but you may find a tool such as Tinkertool or Pathfinder useful. Set the preferences to show hidden files. Be careful not to move, delete or edit any other hidden files unless you know what you're doing as otherwise you can break things.

On Windows

[Thanks to Susan from the WW list for this information and screen shot.]

If you download the file to a computer running Windows you will have trouble finding it as Windows hides files whose filenames begin with a dot. You can edit hidden files on Windows though, provided you can find them.

Find invisible files on Windows 2000.
  1. Open File Explorer
  2. Go to Tools — Folder Options… and click on the tab "View".
  3. Make sure that the option "Show hidden files and folder" is checked.

Be careful not to move, delete or edit any other hidden files unless you know what you're doing as otherwise you can break things.

Create a new .htaccess file

Use a plain text editor such as Notepad (not Word) or TextEdit to create a document called htaccess.txt on your computer. Don't add the dot at the start of the filename or it may become invisible. Upload that file to your server, then rename it to .htaccess. Make sure you add the . (dot) at the start of the file name and remove the .txt extension. Be sure to upload it in ASCII format, not Binary.

FTP software such as Interarchy may allow you to directly create a new file on the Server. See the Listing menu — Create File.

The .htaccess file can go in the root directory and it will then also affect all directories below it. Each other directory may also have its own .htaccess file.


Let's say you've moved a file or directory, or both: www.example.com/training/test.html is now located at www.example.com/learning/newtest.html. You want visitors to end up at the correct page, even if they use the old address.

Open the .htaccess file and enter this on one single line:

redirectpermanent /training/test.html http://www.example.com/learning/newtest.html

Note that this is search engine-friendly, too. Search engines will change the links in their index to the new link on the basis of the redirectpermanent directive. More info: httpd.apache.org/docs/mod/mod_alias.html#redirectperm.

Parse PHP in .html files

Perhaps you have have been learning PHP and want to include some commands in existing html files. The books will tell you to rename those files with a .php extension. Rather than renaming all your files you can use the .htaccess file to tell the server to allow html files to include php. More info: www.desilva.biz/php/phpinhtml.html.

Allow SSI in .html files

Tip provided by Deb from the WW list and rewritten by Miraz.

You may be on a server that requires files to end in .shtml for Server Side Includes. Here is a tip if you do not wish to use the .shtml extension, or if you have added Server Side Includes to existing .htm or .html files. Add the following to your .htaccess file:

AddType text/html .shtml .shtm .htm .html

AddHandler server-parsed .shtml .shtm .htm .html

You can add whichever extensions are relevant.

Files which must be parsed by the server before being displayed may not load as quickly as standard pages. If you use this code in your.htaccess file, the server will parse all .html and .htm pages, including those that do not contain any SSI includes. This could significantly slow the loading of pages which do not use the includes. Be cautious if your pages hold extensive graphics. [Deb mentioned she had not seen slower load times.]

Allow or prevent directory browsing

Tip provided by Deb and rewritten by Miraz.

A good way to increase security on your site involves the .htaccess file. You can override server settings to allow or prevent directory listing.

Prevent directory browsing

An unintended directory listing.

Suppose you have a directory which doesn't have a default file (index.html), such as a folder of images, for example. A visitor may enter an address ending with a / and see a list of all the files in the directory.

You can prevent directory browsing by adding this line to your .htaccess file:

IndexIgnore */*

Allow directory browsing

There may be times when you want or need to allow visitors to browse a directory. For example, you may need to allow access to files in a directory for downloading purposes on a server that is configured to not allow it.

Many servers are configured so that visitors cannot browse directories. In that case visitors will not see the contents of the directory but will instead get an error message.

You can override the servers settings and allow directory browsing with this line:

Options +Indexes

Password protection

Tip provided by Sheila from the WW list and rewritten by Miraz.

You can password protect individual files with .htaccess. It's usually done directory-wide with <Directory> but you can use <Files> to specify a single file:

<Files secret_file.html>
    AuthType Basic
    AuthName "Team Page"
    AuthUserFile path_to_password_file
    Require user username

This only protects the single page; all the files that it is linked to are not protected. More detailed information: httpd.apache.org/docs-2.0/howto/auth.html.

Block various bots

[Stephanie of Glenfinnan Web Hosting supplied this information which was rewritten and amplified by Miraz with reference to: www.webmasterworld.com/forum13/687.htm. Stephanie said: You can block various bots with the .htaccess file, Gini and someone else [from the WiseWomen list] posted their list of spam and spider bots [6 Kb txt file] awhile back.]

Spambots frequently visit our sites for various nefarious purposes. You can block them like this:

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} ^.*Ants.*$ [OR]

RewriteCond %{HTTP_USER_AGENT} ^.*attach.*$ [OR]

[Many more similar lines …]

RewriteCond %{HTTP_USER_AGENT} ^.*Widow.*$ [OR]

RewriteRule /* http://www.fbi.gov [L,R]

The last line sends them off to the FBI, but you could use any URL you wish. Alternatively you could just send them to a standard error page:

RewriteRule ^.* - [F]


The .htaccess file is a very powerful file which can keep visitors away or send them elsewhere, protect pages and directories with a password, allow you to include PHP and SSI within pages which have a .html extension and prevent or allow directory browsing. Handle the .htaccess file with care but use this information as a starting point for further exploration.

Be sure to always test your site after making changes to the .htaccess file, and have fun experimenting.



Photo of Miraz

Miraz Jordan is a writer, web designer and trainer of repute in New Zealand. She contributes the Newbies Guide and other articles to New Zealand's Macguide magazine and produces her own Mac Tips newsletter and other materials. Currently she's working on a book. Her popular Mac Tips and online tutorials are available at http://mactips.info.



Learning Podcasting by Carolyn Wood.


Complete list of past tutorials.


Printable version of this page

Link to us

Join the discussion lists

passionate about the web