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.

Example,

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.

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

How to Create and Use Cookies in PHP

Posted on 8 years ago

Bhumi

How to use Template Parameter in Joomla

Posted on 12 years ago

Bhumi

Learn React JS with simple Application

Posted on 8 years ago

Bhumi

How to use Micro Data in HTML5

Posted on 9 years ago

Bhumi