htaccess - Basic User Authentication
User Authentication, Bandwidth Protection and Password protected directories can all achieved using a .htaccess file on a server running Apache software. It is the standard and most stable method used to protect files or access to directories. Applying directives is simple, updating and managing databases requires a little more knowledge and usually involves the integration of additional scripts coded in server side languages such as Perl or PHP.
A .htaccess file is a simple text file and can be created with any text editor. The file must be named and saved as ".htaccess", lowercase and without the quotes. Not htaccess.html, just .htaccess. htaccess is the extension. If you have never created or used a .htaccess file, create one and upload it to a temporary test directory. Because it is a text file, it must be uploaded in ASCII mode. Once uploaded, it may not appear in your directory listing, because any files that begin with a period are treated as hidden files. If you're using WS_FTP as your FTP client, you can set a command that allows you to view all files, including hidden files.
To view all files using WS_FTP, from the top menu bar select :
Sites -> Organize sites -> Right click on site name -> Properties -> Start up Tab
In the "Remote file mask" text box enter "-al". That's "dash A L" without quotes. That's a unix command for "list all files". Now see if you can find a .htaccess file. Being able to view the file in the directory listing is not necessary, but it may prove useful if you run into any problems. Whether it's visible or not, it will function.
Custom Error Pages
The simplest directives you can add to a .htaccess file are used to redirect the user if errors are encountered. For example, if a file is not found, either becuase it's been deleted from the server, or because the visitor mistyped the URL, the server returns a 404 Page not found error. Using an error directive, you can instruct the server to server up a page of your choice. The syntax is as follows :
directive error-code file
In this case, the directive we want to use is "ErrorDocument". The error code is 404. And the file is the absolute or relative path to the file that should be displayed when a 404 error is encountered or a quoted comment. In your.htaccess file, add a line that looks like the following :
ErrorDocument 404 /missing.html
Make sure that missing.html (or the file you're redirecting the visitor to) exists. Some of the most common error codes and their explanations can be found here.
Each directive you add should be on a new line. For example :
ErrorDocument 401 http://www.yourdomain.com/auth_required.html
ErrorDocument 404 /missing.html
ErrorDocument 500 "Server 500 error encountered"
|