How to Protect a Webpage with a Password

Suppose you want to protect the sub-page “secure” of your homepage with a password; that is you want that the visitors are asked for a username and a password when accessing a webpage:

  • https://people.math.ethz.ch/~USERNAME/secure/

To do that you first have to create the directory “secure” with the correct permissions:

mkdir ~/www/secure
chmod 755 ~/www/secure

Then you have to create a kind of configuration file in that directory, named .htaccess with the following content (you can use emacs, xemacs, vi, … as editor)

AuthName "This is my protected page..."
AuthType Basic
AuthUserFile /hg/w/www1/users/USERNAME/www/.htpasswd
require valid-user

and change the permissions as follows:

chmod 644 ~/www/secure/.htaccess

As last step, you have to create a list of user(s) with password(s) (see the directive AuthUserFile above). To do that just enter the following command:

htpasswd -c ~/www/.htpasswd user1
New password: ******
Re-type new password: ******
Adding password for user user1

The option “-c” has to be used only the first time, since it will create the file .htpasswd. If you want to add more users, use the same command without the “-c”:

htpasswd ~/www/.htpasswd user2
New password: ******
Re-type new password: ******
Adding password for user user2

and check the permissions again:

chmod 644 ~/www/.htpasswd

Author

Posted on
in Password,Web

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PROTECT YOUR BRAINWORK.

Recent Posts

Trending

Categories

Recent Posts