Password protect a web directory using .htaccess

Sometime, you might want to give the privilege to some authorized users for browsing a website or the sub-directories of a website; but block the others from being able to access it. There are actually several ways to accomplish such goal. “Password protect a directory using .htaccess and .htpasswd” is one of the method to get the work done. By doing so, when the user going to browse such password protected directory, he will be prompted a small window that required him to enter the valid username and password before he can access into that password protected directory.

There are just several simple steps as specified below for setting up a password protected directory:

1) Create a .htpasswd file in a directory by using Linux shell command (E.g. the directory /home/thomas/):-

  • # htpasswd -c /home/thomas/.htpasswd your_htpasswd_username
  • Next, you will be required for entering the password.
  • Once you have entered your password, the .htpasswd file should already be created at /home/thomas directory.

2) Create a .htaccess file (i.e. with the settings as shown below) in the directory that you want to have “password protection”:-

  • AuthName “Login Authentication”
  • AuthType Basic
  • AuthUserFile /home/thomas/.htpasswd
  • <Limit GET POST PUT>
  • Require valid-user
  • </Limit>

That’s all for the steps required; of course, you can move the .htaccess file as created above to any web directory in which you hope to give the “password protection”.

January 19th, 2009 @ 10:56 AM • Filed under Apache