Monday, August 10, 2009

Set password protection for you web pages


Here is an simple scenario that comes to my mind. I created a website but I would like to protect some sub-directories from viewing by anonymous users. For example, you might created web pages to store all your bookmarks but you don't want to share it to the public but only limit to some close friends. Or, you are a instructor and you would like to share course website only to the students who attend the class. Okay... the solution is simple, you would like to create a password-protect directory through http server.

You will need two files to achieve this goal. First, you need a file, .htaccess to store the user name and authentication type. Secondly, you need a file, .htpasswd, to store the password of the user you created in .htaccess file.

Filename:.htaccess

AuthType Basic
# You can customized your authentication name
AuthName "Chucheng's Research Authentication"
# Where the .htpasswd located
AuthUserFile /home/chucheng/www/Research/.htpasswd
# Assign user name here
Require user chucheng


You can then generate the .htpasswd by typing the following code:
# The same username you used in .htaccess
$ htpasswd -c /home/chucheng/www/Research/.htpasswd chucheng


You can update the .htpasswd by typing the following code:
# The same username you used in .htaccess
$ htpasswd /home/chucheng/www/Research/.htpasswd-users chucheng


You are done! Enjoy your password-protected website :)


No comments:

Post a Comment