AnOwnSite header

How to use htaccess to password protect parts of your site


htaccess password protection

There's an easy solution in htaccess to password protect certain parts of your web site. This solution is typical for password protection for directories.

First, you have to create a text file to contain passwords and name it .htpassword (yes, it's only an extension). You should place this file on your server outside the public area in a safe directory, so it will not be accessible publicly.
Also you should upload this file, as with htaccess, in ASCII mode, not binary!

In the file you just created, you have to enter a user name and a password like this:
cstevens:applepie
cstevens is the user name here and applepie is my password. If I want to enter your password protected directory, I will be prompted to enter 'applepie'.


Next, you need to tell the server which part of your site is password protected, where to find the password file and who can use it.

Open your htaccess file and add the following lines:
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /usr/local/yoursite/safedir/passwords
Require user cstevens
The path thas is shown after AuthUserFile is the path to the htpassword file and that must be correct to have this working.

Require user cstevens means, that only user cstevens will have access to the password protected area. If you want more users to be able to access your private area, you will have to specify them there or replace that line by:
require valid-user

The value of AuthName can be anything you want, eg. Enter Password. It's just a name for your password protected area.




Click to return to htaccess possibilities