By Engine23

Password protect a folder in Linux using .htaccess

What’s the easiest way to make an .htaccess file in Unix/Linux so that a directory is password protected? Suppose that your home directory is /home/matt and all your webstuff is in /var/www/protected/ . Follow these steps:

  1. Make an .htpasswd file. The htpasswd command in Unix does this. You should put the password file outside of your web directory. So a command like "htpasswd -bc /var/password/.htpasswd admin mysecretpassword" will create a new file using a username of admin and a password of mysecretpassword into the file /var/password/.htpasswd . At the prompt type "cat /var/password/.htpasswd" it will show you something like: "admin:E.mx8CsZffRI6".
  2. Make an .htaccess file located at /var/www/protected/.htaccess and use the following
    AuthUserFile /var/password/.htpasswd
    AuthName EnterPassword
    AuthType Basic
    require valid-user