In my previous articles, i already discussed what is .htaccess and their applications in web development. In this short tip, i am going to tell you how to secure you folders through .htaccess File.
Password Protect a Folder Through .htaccess
Create one .htaccess file in a folder, which you want to password protected.
Write these codes on .htaccess File
1 2 3 4 |
AuthName "Only Authorized Admin" AuthType basic AuthUserFile /var/www/example/.htpasswd require valid-user |
AuthUserFile : Defines the the location of .htpasswd from root. This file contains the username and password.
AuthName : It defines the title of popup authentication. So in my case if popup appears, it shows the title Only Authorized Admin.
require valid-user : It means any valid user is allowed.
If i want only specific user then i write their name
1 |
require user raj |
.htpasswd File Creation
Next step is to create .htpasswd file. .htpasswd file contains username and password in the following format.
1 |
username: encrypted password |
You can use htpasswd generator to create .htpasswd file. Enter username and password. It generate the combination. Paste the generated combination in .htpasswd file.
Now everything is done. Try to open the files in that folder, it will open the popupbox for authentication. If you enter the credential correct, it will allow. Otherwise it will give 401 Unauthorized.
Unauthorized
This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn’t understand how to supply the credentials required.