Server101

Webhosting & eCommerce

htaccess Examples

.htaccess files are set passwords on directories and custom error pages. A .htaccess file is a plain ASCII text file resides in any directory of your website. It can be edited by any regular text editor and uploaded via FTP like any other file you upload to your website. Please Note: .htaccess files must be formatted properly. They can not have additional spaces or carriage returns in the middle of strings please take care when editing your .htaccess file and make a backup before you begin.

Note to Microsoft FrontPage users:
It is recommended that you do not edit your .htaccess file if you are using Microsoft FrontPage to manage your website. FrontPage modifies the .htaccess file directly so editing this file may cripple your website. For FrontPage users you can set permissions through FrontPage. Further information can be found in the help file accompanying the program.

Password Protecting Directories
You need to make a file called .htaccess with the content as below, then put it in the directory you would like to protect. upload in ascii mode. replace "/full/path/to/htaccess/location/" with your full homedir and directory to your .htaccess file which can be found in the 'Your Details' Page inside the Member Services. To use multiple passwords you will need to use the .htaccess below and also a .htpasswd that will contain the username and password listings

<Files ~ "^\.(htaccess|htpasswd)$">
Order deny,allow
deny from all
</Files>

AuthUserFile /full/path/to/htaccess/location/.htpasswd
AuthGroupFile /dev/null
AuthName Password-Protection
AuthType Basic

<Limit GET POST>
Require valid-user
</Limit>

The .htpasswd file should contain the list of usernames and passwords with the only requirements of each record on a new line and the password must be encrypted. To encrypt your password(s) you can use the facility here.

Your .htpasswd file should look something like ..

username1:2M47nSlXVLRCY
username2:2M47nSlXVHGDQ

Turning Directory Listings Off
If you have a folder(s) that do not contain an index page(index.html , index.php etc) and you do not want the contents of the folder displayed in the browser, you can turn off this feature.

You would add the following line to your .htaccess file :
IndexIgnore *

Add custom error pages to your website.
The most common error message most users receive is the 404 file not found page which is usually received when a user mistypes a URL or a page is incorrectly linked or the file is no longer there. It looks something like this ..
---------------------------------------------------------------------

Not Found

The requested URL /bad_or_mistyped_url.html was not found on this server.

---------------------------------------------------------------------
If you would like to change this default page create your own custom error page save it to your website and add the following line to your .htaccess file.

ErrorDocument 404 /your404errorpage.html (all on one line)

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. Of course you can have as many custom pages for any of the HTTP errors that may be applicable to your site. Some of the common errors are listed below.

401 Unauthorized
Usually a return error when you fail to authenticate in a password protected area. (your password was incorrect)

403 Forbidden
You tried to access a URL for which you don't have permission. (your IP Address has been denied)

500 Internal Server Error
There is an internal server error on the web site. Most of the time this is a badly written or misconfigured script.

If you were to include all of these custom pages your .htaccess would look something like this.

ErrorDocument 401 /custom401.html
ErrorDocument 403 /custom403.html
ErrorDocument 404 /custom404.html
ErrorDocument 500 /custom500.html


Note for Internet Explorer and 404 Error Pages
There are a few changes that may need to be made to your IE browser in order to get your 404 page to display correctly.

Under Tools-> Internet Options-> "Advanced" tab:
Un-tick "Show friendly HTTP error messages"

Internet Explorer versions 5+ will ONLY show your custom error pages if the 404 error page has a greater size than 512 bytes.

A list of available server errors
200 OK
201 Created
202 Accepted
203 Non-Authorative Information
204 No Content
205 Reset Content
206 Partial Content

300 Multiple Choices
301 Moved Permanently
302 Moved Temporarily
303 See Other
304 Not Modified
305 Use Proxy

400 Bad Request
401 Authorization Required
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable (encoding)
407 Proxy Authentication Required
408 Request Timed Out
409 Conflicting Request
410 Gone
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type

500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported