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

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.

Share on Facebook0

Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

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 *

Most Popular Posts

Learn Responsive Web Design Online

Posted on 4 years ago


Really! PHP touches the 200 Million Mark

Posted on 4 years ago

John Pitt

An Introduction to Gulp JS

Updated 3 hours ago


Getting started with Laravel

Updated 2 days ago


How to use Git init command

Updated 1 week ago


The Reader’s Poll – June 2015

Posted on 2 years ago


The Reader’s Poll – August 2014

Posted on 2 years ago


The Reader’s Poll – June 2014

Posted on 3 years ago


The Readers’ Poll – May 2014

Posted on 3 years ago


The Readers’ Poll – August 2013

Posted on 4 years ago