How to password protect a directory with .htaccess

To protect a directory on the web follow these simple steps:

1. Open a .htaccess file within the directory you wish to protect

  1. vim .htaccess

2. Insert the followoing code:

  1. AuthUserFile /route/to/directory/.htpasswd
  2. AuthGroupFile /dev/null
  3. AuthName “Custom text to show in pop-up”
  4. AuthType Basic
  5. <Limit GET>
  6. require valid-user
  7. </Limit>

3. Now we need to create the username and password:

  1. htpasswd -c /route/to/directory/.htpasswd username

4. You should be prompted for a password.

5. Browse to the directory in your browser and you should be prompted for your username and password.

That’s it, make sure to actually change the “/route/to/directory” to an existing directory.