.htaccess Basics

A .htaccess file is a special file used to control certain aspects of your website. With .htaccess you can:

  1. Password protect directories
  2. Point domains and subdomains to specific directories
  3. Block traffic to your website
  4. and more

When you place a .htaccess file in a directory, it will affect that directory and all directories below that one.

To create a .htaccess file:

  1. Create an empty text file (named htaccess.txt) in Notepad
  2. Add the contents of your .htaccess file
  3. Upload the file to your package in the appropriate directory for what you want to do
  4. Rename the file .htaccess
  5. Note – You can also save the file directly in NotePad as “.htaccess” by putting quotes around the name during the save.  But if you do that be sure your FTP program will still upload it in ASCII format.

Some of the simple “one liners” are:

  1. To disallow the showing of default index pages   Options -indexes
  2. To turn indexes back on in a lower directory   Options +indexes
  3. To redirect requests to a document or directory   Redirect /olddirectory http://domain.com/newdirectory/
  4. Have your HTML (.htm) pages processed as PHP (.php) pages   AddHandler application/x-httpd-php .php .htm
  5. To use a different startup page   DirectoryIndex filename.htm
  6. To force all files of a certain type to download rather than open   <files *.pdf>ForceType applicaton/octet-stream</files>
  7. To use a custom error page   ErrorDocument 404 /errorpage.htm
    The error codes you can use, and what they mean, are listed below:

    • 401 – Not Authorized – example is a failed htaccess/htpasswd request
    • 403 – Forbidden – example is requesting a default directory index if Options -indexes
    • 404 – Not Found – example is an invalid file name
    • 500 – Internal Server Error – example is an invalid htaccess file

Additional specific .htaccess functiionality can be found in many of the Tips & Scripts topics on this page.


Read previous post:
Helpful WordPress Code Snippets

Theme / Template A theme is the folder that holds all of your files. For example, the new WordPress theme...

Close