If you wanted a specific directory in your site to be available only to a specific people or if your site is in staging mode, so you can use password protecting method to your site which can be possible by htpasswd file.
The password file is used to store valid username and password information which require accessing site/directory. Basically, Apache provides a program to create a password file and the program called htpasswd which is automatically installed in bin which is the subdirectory of the apache like
1 2 3 | C:/Apache/bin/htpasswd.exe |
Steps to Create Htpasswd file
Below are the steps to create htpasswd file
Step 1: Create .htpasswd file
First of all, you need to create a file called .htpasswd and place the username and password (encrypted) into your .htpasswd file.
Example,
Username is admin and password are 123456 so the htpasswd file would be like
1 2 3 | admin:9dKtKHPyz51Vs |
Here, UserName comes first followed by the Password and password must be in encrypted form.Now, upload .htpasswd file into a root directory.
You can generate encrypted password from here
Step 2: Create .htaccess file
Create a new .htaccess file and place the following code
1 2 3 4 5 6 7 | AuthName "Restricted Area" AuthType Basic AuthUserFile /home/site/.htpasswd AuthGroupFile /dev/null require valid-user |
Next, upload .htaccess file and test your URL by accessing it.
The AuthName is the name of the area you want to access.AuthType Basic is because we are using basic HTTP authentication.AuthUserFile is the root path of the server to the htpasswd file.require valid-user means the only valid user can access the directory from the entire list of users.
you’re done!
NOTE:Above code will only work on Web servers which support Htaccess. If you don’t have an idea that your server supports Htaccess, you should contact your hosting provider.
Suggested Reading:
Set DirectoryIndex using .htaccess
To set Expire Headers using .htaccess
I hope you have enjoyed this tutorial. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (1)