Htaccess

Optimize the use of your htaccess by customizing your error pages, limiting the number of visitors on your site, disabling directory listings, automatic downloading of text files and more!

Making the htaccess file

Open your text editor and let’s make a sample htacess file. Leave the file blank and save it as “a.htaccess” (with quotations). Copy andd paste the features you’ve chosen for the htaccess file you want to use (read below of the articles) and upload it to your server using ASCII mode in the directory you want to use it in. Once uploaded, CHMOD it to 644 and rename the file as “.htaccess” (without quotations).

Now read below for the functions you want to use in your htaccess file.

Custom Error Pages

When vising a website, errors occur because of broken links, permissions or any number of causes. Common errors are:

  • Error 401 – Authorization Required (you failed to enter the required username and password for the URL)
  • Error 403 – Forbidden (you are not permitted to view this site)
  • Error 404 – Not Found (the URL or file was not found on the server)
  • Error 500 – Internal Server Error (an internal error occurred on the server)

To customize your own error messages and replace the boring ones the server makes, you can do so by customizing the text messages or page generated when the error occurs.

For custom text messages:

ErrorDocument 403 "Sorry, but you are not authorized to view this page.
Please press the Back button on your browser."

For custom HTML error pages:

You can customize your HTML error pages in any way you like. You can use images, style sheets, or even include a form, basically anything you can do on an HTML page.

ErrorDocument 404/404.html

where 404.html is the page you’ve created to inform the visitor of the error and possibly even provide him a link back to the main site.

Limiting the Number of Visitors

“Why would I want to do such a thing?” you ask. Well, you can use this function in your site if it is the type that frequently runs out of bandwidth and having to pay extra for it. Or you can use it for a promo when you’re giving away free stuff like “Limited offer only. For the first 100 visitors! ” or something to that effect. The list is endless!

MaxClients
<number of max clients>

and change the <number of max clients> to the maximum number of visitors you want to view your site.

Disabling Directory Listings

Not all of our directories have an index.html or index.php file. So what happens when a visitor view one of our folders without an index file? THe browser displays all of the files in that directory, which is a security risk. To prevent this, create an htaccess file with the following code and upload it to the directory you wish to safeguard.

Options -Indexes

Automatic Downloading of Text Files

When a text file is requested via browser, it is not automatically downloaded, as with zip files. Rather, the contents can be viewed by all visitors. To prevent this, use this code:

<files filename.txt>
AddType application/octect-stream txt
</files>

By using the above code, it means all text files in your web site will be downloaded and not viewed. If you only want a specific file to be downloaded, use this:

AddType application/octect-stream txt

and replace filename with the name of your desired file.

Creating Your Error Log

To create an error log for you site errors, use the following code:

ErrorLog /sitelogs/error_logs.log

Change the path directory and file name as desired, but don’t forget to start your path with a forward slash.

On the next tutorial, I’ll include preventing image hotlinking, password protection of directories, IP allowing and banning, redirection, and more!

December 20th, 2005 | 1 Comment »

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Htaccess”


[...] part of the tutorial, or you don’t know how to create the .htaccess file yet, I suggest you read the first part of the tutorial [...]

by Htaccess – Part 2 » Swordian » Archive — March 14, 2007 @ 9:08 am

Leave a Reply