Password protect directory with .htaccess and .htpasswd

If you have some files to share among friends in your web server, you can use .htaccess to password protect your directory. To enable this password protect directory feature without any web control panel (ex: CPanel, Ensim, DirectAdmin or etc) is a bit troublesome. No worry, the tutorial below will teach you how to password protect directory using shell or CPanel.

To password protect your website folder with shell command:-

- Create a file name .htaccess in the folder that you want to password protect with the content below:

AuthName "Password Protect Directory"
AuthType Basic
AuthUserFile /home/username/.htpasswd
Require valid-user

In shell type:

htpasswd -c /home/username/.htpasswd your_desire_username

You will be prompt for a new password.
- Once you enter your password, file with name .htpasswd will be created at /home/username directory and now your website folder has been password protected
- Open your browser and try to access your password protected folder. It should now ask you to login now.
To password protect directory with CPanel Hosting Control Panel:-

  • Login into your CPanel and click on Password Protect Directories
  • Once you click on Password Protect Directories, you will see a list of directories
  • Click on the directory that you wish to password protect
  • Fill in a Username and Password at the bottom of the page, and click Add / modify authorized user
  • Once user created successfully, just click “Go Back”
  • Now, Check the Directory requires a password to access via the web
  • Fill in Protected Resource Name, actually this is just the message that will show in the login window
  • then, Highlight the user you just created from the Active Users list and click on Save button below the Protected Resource Name
  • You are Done! The directory you just select has been protected by password.
  • To ensure you directory has been password protected, launch you browser and visit the folder, if browser prompt you to login, your directory has been protected by password!


Leave a Reply