How to Password Protect directories with .htaccess & .htpasswd on cPanel

Sometimes we need to protect certain folders that we would like to access from time to time. One way to do that is by using .htaccess and .htpasswd.

This tutorial is going to show you how to protect folders using cPanel on an Apache based server.

Protecting directories with cPanel

The first thing you will need to do is login to your cPanel account and locate the icon labeled  password protect directoriesunder security.

password protect directories

Once you have clicked on the icon a popup will appear with some options – just select Web Root and go.

You will see a list of directories, click on the directory that you want to protect – in this case “test“.

list of directories in public_html

After clicking on the folder you want to protect, it will take you to a screen like this:

security settings cpanel

First fill out the details under security settings and hit save.

After you hit save it will take you to a screen that says

The access permissions for /home/username/public_html/test/ have been set.

This just means that a .htaccess file has been generated and added to that folder with the relevant information in it. Select the back button.

Now fill out the Create User section and hit “Add/modify authorized user”.

That’s it now you have protected that directory.

If you type in the path to your directory – example: http://sitename.com/test you should see a pop up asking you for credentials.

It doesn’t seem to be Working – Error 404?

Some users may receive an error 404 and to fix that you need to go to the directory that you protected and included an additional line in the .htaccess file.

Open up file-manager via cpanel.

When you see the popup check the option that says “show hidden files (dotfiles)” and then open up the folder that you protected.

Inside you will see a file called .htaccess.

Right click and select code edit.

This will open the file up for editing.

At the very top of the file include the following line.

ErrorDocument 401 default

Then hit the save button – top right.

Now when you type your directory path in, it should prompt you to enter your username and password.

authentication required

Meet the Author

Matthew Horne

Matthew Horne is web developer who specializes in optimized development. He also builds custom solutions instead of reverting to plugins. Matthew Has a strong understanding of PHP, JavaScript, jQuery.

1 comment… add one

Leave a Comment