Password protect folders
You need 2 files, named .htpasswd and .htaccess to password protect a website or folder; password protect with htaccess and htpasswd.
.htpasswd
- Generate content for .htpasswd file. Google around for a htpasswd generator... http://home.flash.net/cgi-bin/pw.pl
- Enter a desired user name and password, hit Go! You get username:passwd, something like John:Asd34Fgh6vBgt
- Save it into a file .htpasswd.
- Each username:passwd combination should have it's own line in the .htpasswd file.
- .htpasswd should not be fetchable with a browser, it should be aside from web folders, hide .htpasswd somewhere in /home/john/
.htaccess
AuthUserFile /home/john/.htpasswd AuthGroupFile /dev/null AuthName "foo" AuthType Basic <Limit GET> require valid-user </Limit>
Place this .htaccess file in to the folder you want protect, sample: /var/www/sample.com/my-protected-folder/.htpasswd (http://sample.com/my-protected-folder/)
Ensure that the Apache conf file something /etc/apache/apache.conf contains a declaration:
<Directory "/var/www/sample.com/my-protected-folder"> AllowOverride All </Directory>
Scenario 2, over the console
Create a new .htpasswd file
$ htpasswd -c /path-to-my-secret-place/.htpasswd name-of-user
Update existing or add users
$ htpasswd /path-to-my-secret-place/.htpasswd name-of-other-user
Instead of using .htpasswd you can configure httpd.conf or apache2.conf, like this:
<Directory /var/www/mydomain.com/folder/secretfolder> AuthType basic AuthName "foo" AuthUserFile /path-to-my-secret-place/.htpasswd AuthGroupFile /dev/null <Limit GET> Require User name-of-user name-of-other-user </Limit> </Directory>
Solving a htpassword problem
- Apache authentication uses the modules mod_auth and mod_access. Usually they are set up and enabled by default install.
- 404 - the folder to be protected is missing or it's name is mispelled.
- 500, internal server error - doesn't find/understand the file .htpasswd, the path may be wrong.
- 401, Authorization Required, but anyway thing doesn't work - .htpasswd is found, digg further, probably Options Indexes, Options -Indexes, Options +Indexes.
Tags: .htpasswd, htpasswd generator, htpasswd tutorial, htpasswd htaccess, htpasswd file, htpasswd example, htpasswd howto.
2010
Web Management - website set up, domain administration, DNS administration, server administration, site maintenance.