I was working with .htaccess since a long time but I used .htaccess with .htpasswd for the first time in my 5 years of development. I did not came across any requirement like this before so never used before. Recently I need to implement password protected pages for one of my client. So I searched for it and came to know that we can do it using .htaccess and .htpasswd.
In .htpasswd, all username and password will be saved. Passwords are in encrypted forms. In .htaccess file, we are giving .htpasswd path to search for username and password. and than validate using it. Here we go with the code of .htaccess and .htpasswd.
Password is rakshit but it is in encrypted form.
AuthName "Restricted Area" AuthType Basic AuthUserFile /home/.htpasswd <Files test.php> require valid-user </Files>
SO now when you try to open test.php file, it will ask for authorization. When you enter rakshit/rakshit, than only it permits you to view the page. Isn’t it easy ?
Note: Remember, try to keep your .htpasswd file outside public_html folder. It is not required but keeping the file outside of public_html folder is safe. Just a security reason.
Password Protection using .htaccess and .htpasswd, How to Protect pages using .htaccess, Password Protection using .htpasswd, Password Protection using .htaccess, How to protect pages using .htpasswd, Generate Password of .htpasswd, for all of above, you will find the answer here.