The .htaccess file is an ASCII text document that can be placed in any directory on your site. It can be used to control access to files and directories, and customize some server operation in your site. A .htaccess file can be created in any word processor but must be saved as text only. You must use FTP software in ASCII mode to upload or edit your .htaccess file. For the examples provided here, place the .htaccess file in your root directory.

There are a variety of functions that you can control using .htaccess some of the more useful of these are explained below:

Custom Error Messages.

Add the following to the .htaccess file::

ErrorDocument 404 /notfound.html

After "ErrorDocument" specify the error code, followed by a space, and then the path and filename of the .html file you would like to be displayed when the specified error is generated, each specific error code is detailed below with the recommended codes to be used in the .htaccess file in bold :

200 OK
206 Partial content
301 Document moved permanently
302 Document found elsewhere
304 Not modified since last retrieval
400 Bad request
403 Access forbidden
404 Document not found
408 Request timeout
500 Internal server error
501 Request type not supported

Using the codes above your error section of the .htaccess file should look like this:

ErrorDocument 301 /notfound.html
ErrorDocument 400 /notfound.html
ErrorDocument 403 /notfound.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /notfound.html
ErrorDocument 501 /notfound.html

Redirect to a Different Folder.

Add the following to the .htaccess file:

RewriteEngine on
RewriteRule ^/oldfolder(.*)$ /newfolder/$1 [R]

This redirects the user from /oldfolder/anyfile.html to /newfolder/anyfile.html, when the .htaccess file is uploaded to the otherwise empty "/oldfolder" directory.

Denying User Access.

Add the following to the .htaccess file:

<Limit GET>
order allow,deny
deny from 000.00.00.
deny from 000.000.000.000
allow from all
</Limit>

This is an example of a .htaccess file that will block access to your site to anyone who is coming from any IP address beginning with 000.00.00 and from the specific IP address 000.000.000.000 . By specifying only part of an IP address, and ending the partial IP address with a period, all sub-addresses coming from the specified IP address block will be blocked. You must use the IP addresses to block access, use of domain names is not supported

Redirect a Machine Name.

Add the following to the .htaccess file:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for machine.domain-name.net
RewriteCond %{HTTP_HOST} machine.domain-name.net $
RewriteCond %{REQUEST_URI} !machine/
RewriteRule ^(.*)$ machine/$1

This will redirect requests for the machine name machine.domain-name.net to the directory machine on the site domain-name.net.

Different Default Home Page.

Add the following to the .htaccess file:

DirectoryIndex filename.html

Then a request for http://domain-name.net/ would return http://domain-name.net/filename.html if it exists, or would list the directory if it did not exist.

To automatically run a cgi script, add the following to the .htaccess file:

DirectoryIndex /cgi-local/index.pl

This would cause the CGI script /cgi-bin/index.pl to be executed.

If you place your .htaccess file containing the DirectoryIndex specification in the root directory of your site, it will apply for all sub-directories at your site.

Preventing Hot Linking.

Add the following to the .htaccess file:

# Rewrite Rule for images
RewriteCond %{HTTP_REFERER} <URL of page accessing your domain>
RewriteRule ^(.*)$ http://<same as above URL>

You would replace the <URL of page accessing your domain> above with the domain name and path of the page that is referring to your domain. For example: www.theirdomain.com/users/mypage/

The RewriteCond directive states that if the {HTTP_REFERER} matches the URL that follows, then use the RewriteRule directive. The RewriteRule directive will redirect any reference back to the referring web page.

Using the above you should, safely be able to publish your sites on the internet knowing that you will not be privy to bandwidth thieves via hotlinking and also, that you will not lose any traffic through pages that are 'not found'.

Article Written By Lee.

http://www.geoscripting.com