Back to Top

Password Protect a Directory Using .htaccess

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


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.


Username is admin and password are 123456 so the htpasswd file would be like


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

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)

  1. I need to by way of thanking you with this very good examine!! I definitely enjoyed just of it. I have your site bookmarked to look at a new challenge you post.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

To access windows shared files in ubuntu

Posted on 6 years ago


Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago