htaccess htpasswd Ubuntu

In this example, I assume that you want to all user have to input username/password when access http://localhost/test. And web root directory is /var/www/test.

Make sure that your web server was enable mod_rewrite. And your virtual host allow override all:

To use htpasswd command , you need install apache2-utils:

To implement it, firstly, you create .htpassword file by command:

And then, create .htaccess file:

With content:

Please make note that you have to use absolute path for .htpasswd file.

Done, try a test: http://localhost/test