In the last chapter we had discussed about .htaccess file and the use of .htaccess file. Lets discuss how to create password protected directory or  sub directory through .htaccess file.

First of all you need to create a .htpasswd file, use the same method you used to create a .htaccess file. The .htpasswd file will content the user name and password. The username and password will be as username:password. So the user name and password will be in single line and separated by a colon (:).
And also you need to use encrypted password. Click here to use our encryption tool to generate password for .htpasswd file.

Once you created your .htpasswd file please enter the following codes to your you .htaccess file.
AuthUserFile /path/to/htpasswd/file/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
require valid-user
In the above code change "/path/to/htpasswd/file/.htpasswd" to real path path of your .htpasswd file.

How to create password protect files?

If you want to password protect some file not the entire directory, then use the following code.
AuthUserFile /path/to/htpasswd/file/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
<Files file1.php>
require valid-user
</Files>
<Files file2.html>
require valid-user
</Files>

Use our tool to generate .htaccess and .htpasswd file.

Enter Username Enter Password
Path to .htpasswd file
File names [Leave blank for folder and use coma(,) for multiple files]



Your .htaccess file




Your .htpasswd file




Top