In this tutorial, I will let you know how to use .htaccess to password protect your web pages in simple steps.
There are so many website that protect their some pages by asking you for a username and password. Without entering valid credentials, you can not see the web pages.
Htaccess is known as most secure method of password protection. Using .htaccess you can protect single webpage or entire directory.
You will need to create two files .htaccess and .htpasswd in the directory that you are going to password protect.
For this example, I create a blog directory in /var/www/html/.
Now run the following command to generate .htpasswd file with username and password.
htpasswd -c .htpasswd your-user-name
.htpasswd file should contain :
user:$apr1$c4Ol904J$SmqVIhw53PGDmaVWM9Vhx.
Now add following line of code in the .htaccess file :
AuthType Basic AuthName "Password Protected Area" AuthUserFile /var/www/html/blog/.htpasswd Require valid-user
Now when you run your blog application in the browser it will ask you for username and password.
If your web server does not support htaccess then above steps will not work.
Your htaccess file must be a text file not some other format.