About usContact usSite help
Quick links
Search this site

Allowing password access for individual users

Introduction

The following is a detailed example of how you would set up an htaccess file to allow password restricted access for individual users to part of a Commercial Web Site. If you need to do this for groups of users, please read Allowing Password Access for Groups of Users. You will need to be familiar with some of the Unix commands described in the Using UNIX section to proceed.

Set up the directories

TELNET into your site and create a directory in your docs directory (using mkdir). In this example we'll call it protected. You will also need to create another directory which we will call passwords in the cgi-files directory.

Create the htaccess file

Now we will create an htaccess system that allows password access for a user called Alfred to the protected directory. On your computer, use a simple text editor to create a file called access.txt and insert the following text:

AuthUserFile /cgi-files/www.your-hostname/passwords/.htpasswd
AuthGroupFile /dev/null
AuthName "Restricted Access"
AuthType Basic
<Limit GET>
require user Alfred
</Limit>

... where "your-hostname" is your registered domain name, eg. yourcompany.co.uk or yourcompany.com etc. Note that we are going to place the .htpasswd file in the cgi-files/passwords directory. This is the most secure place to put this file.

N.B. There must be a blank line at the bottom of the access.txt file.

Place the htaccess file

Make an FTP connection to your website and transfer the access.txt file to your protected directory. Remember to FTP the file in ASCII or Text transfer mode - it is to be read directly by the server so it must be in Unix format. Please read the documentation about uploading your pages if this is unclear to you.

You now need to rename the file to make it function. In TELNET change directory to the protected directory and type:

mv access.txt .htaccess

Please note, once you rename the file it will not be shown when you use "ls" to list the directory contents. Instead you can use "ls -a" or "ls -la" to see its details.

You now need to chmod the .htaccess file in your protected directory in order to activate it. Type:

chmod 604 .htaccess

Make the htpasswd file

Change directory to the cgi-files/passwords directory. Type the following at the prompt:

htpasswd -c .htpasswd Alfred

This stage prompts you to enter a password - choose one, but remember that it is case-sensitive. A file called .htpasswd will be created. Again you will not see this file unless you use "ls -a". You do not need to chmod this file.

Test the setup

Copy an existing HTML or other standard file type to your protected directory. chmod it if necessary. Now start up your browser and try to access the directory, or the file within it - you should be challenged for a username and password. If there is no challenge, check that you have performed the chmod step on the .htaccess file. If you get a "Server Error" message, check that the access.txt file has the correct format and has been copied to the correct location and name.

Adding more users

To add further users (for example Batman and Catwoman) you need to edit the "require user..." line in the access.txt file, eg.

require user Alfred Batman Catwoman

You must then transfer the update to the protected directory, rename and chmod as before.

Again the next step is to use the htpasswd command in the cgi-files/passwords directory, but this time without the "-c" switch, eg.

htpasswd .htpasswd Alfred
htpasswd .htpasswd Batman
htpasswd .htpasswd Catwoman

Each user will have a separate password. If you need to extend this to a large number of users, it may be better to set passwords for groups of users. Please read Allowing Password Access for Groups of Users for details.


More Help





*