Password protection with .htaccess
There are two steps to enable password protection at your website
using .htaccess.
Create a password file
The first step is to create a password file. For security, the file contains passwords in encrypted format. Use the htpasswd command to do this.- Log in to your Unix shell account.
-
Create the file in your home folder and add one
user name by typing the following command.
htpasswd -c .htpasswd usernameReplace username with the user name you want to add. You'll be prompted for a password for the new user.
-
To add more users to a password file that already exists, type the following command:
htpasswd .htpasswd usernameReplace username with the user name you want to add. You'll be prompted for a password for the new user.
- To change a user's password, type the same command as in the previous step, replacing username with the name of the user whose password you are changing.
Create the .htaccess file
The .htaccess file is a plain text file that contains special directives telling the web server to prompt visitors for a user name and password.- Create a plain text file called .htaccess either on your computer, or on the Unix server using a text editor like pico.
-
Edit the file to contain the following lines.
AuthUserFile /u/e/example/.htpasswdBe sure to replace /u/e/example/ with the exact path to the password file you created in the previous steps. If you created the file in your home folder, just change "e" to the first letter of your user name, and "example" to your user name. You can also discover the path by typing the pwd command, which outputs the path to the folder you are currently located in.
AuthName "Example display text (change to suit your web site)"
AuthType Basic
Require valid-user
- If you created the .htaccess file on your computer, upload it with FTP to the folder you want to password protect. If you created it in your Unix shell account, make sure it's located in the folder you want to password protect.
