Working with .htaccess

Introduction

The ".htaccess" file is a simple text file that may be located in one or many directories in your virtual server. Traditionally, this file has controlled access to pages in a given directory, but with the newer versions of Apache, some of the server configuration parameters are controllable from the this file as well.

When your virtual server receives a request for a file, it checks in the top directory for a .htaccess file, and then checks each subdirectory down to and including the directory that your file is in. Thus you can set the defaults for all directories in a virtual domain by placing a .htaccess file in the top directory of that domain, or set configuration and access parameters for one or several individual directories independent of each other..

You can use the .htaccess file to:

  • Redirect hits from one URL to another. (Useful when you've deleted or moved a page or a whole site.).
  • Allow or Deny access to specific visitors.
  • Set Custom Error Messages for various operations (e.g. Produce a more more friendly page than "404 Object Not Found".)
  • Set or change a Mime-type if the system file doesn't know about the mime-type you want to use.

    Other capabilities (not covered here) include:

  • Turn on/off some of the server features -- such as automatic directory indexing.

NOTE: Do NOT edit the .htaccess file if you are using MS FrontPage! FrontPage uses the .htaccess file for its own purposes, and editing it may cause errors in your configuration.

Redirects

This is convenient way to direct a browser to a different page if you've re-organized your web-site or otherwise deleted or renamed a page. For example, if you moved a page in somedir from somepage1.html to somepage2.html, the appropriate line to add to your .htaccess file would be:

Redirect /somedir/somepage1.html http://yourdomain.tld/somedir/somepage2.html

Since you get to specify the full URL to the new page, you can bounce the browser to another server if you want

Redirects work for directories as well, ie:

Redirect /somedir http://yourdomain.tld/newdir

  In this case, the requested document was /somedir/foo.html ... since the /somedir part matched the Redirect request, the remaining part of the URL (/foo.html) was tacked onto the redirect, ultimately taking you to http://yourdomain.tld/newdir/foo.html

Access Control

Every now and then, you may find yourself in a situation where you will need to deny certain visitors access to your site. This visitors may be badly behaving bots that request hundreds of pages per minute, or perhaps someone maliciously abusing your resources. In any case, the first level response is to block access with your .htacccess file.

The most common scenario is where you want everyone except a few specific visitors to be able to access your site. Your .htaccess file would start like this (lines in bold text are the actual lines that you would add to your .htaccess file):

order allow,deny
allow from all


Then you follow that with lines to specify which address(es) you want to block:

deny from 192.168.128.14
denies access only to the specific IP 192.168.128.14

deny from 192.168.128
denies access to the entire IP range of 192.168.128.0 - 192.168.128.255

deny from foo.example.com
denies access to anyone coming from the foo.example.com subdomain, but allows people from example.com to still access your site.

deny from .example.com
denies access to all of example.com, including any subdomains

Error Documents

We're all familiar with the generic type of Not Found (404) error page:

File Not found

The requested URL /someone/mistyped/a/link.html was not found on this server.

We've probably all run into a page like the following too:

Oops! You seem to have found a bad link. The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

Often these pages have a link back to the referering page, and might show you the URL that you tried to access. This is an example of a custom Error Document.

Most errors that the web server runs into have error numbers. For example, "Not Found" is error 404. To specify an error document in your .htaccess file, you add "ErrorDocument", the three digits of the error number and the either the error string or the page to go to. The following examples show the three forms; you can call your custom error documents anything you like, we use /<errornum>.html for the sake of simplicity:

ErrorDocument 401 /401.html
ErrorDocument 403 /402.html
ErrorDocument 404 /404.html


You can also specify the error documents in the form:

ErrorDocument 404 http://yourdomain.tld/404.html


Client Error Codes
Number Description
400 Bad Syntax
401 Unauthorized
402 Not Used (Payment Granted)
403 Forbidden
404 Not Found
Server Error Codes
500 Internal Error
501 Not Implemented
502 Overloaded
503 Gateway Timeout

Changing MIME-Types

Changing Mime-types is easy. Just create a .htaccess file that looks like this:

AddType new-mime-type extension

ie;

AddType text/html .txt