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!

[tags]password protect directory, .htaccess password protect directory, .htaccess password protect, password protected directories, cpanel password protect directory, cpanel password protect directories, shell password protect directories, shell password protect directory, .htaccess protect directory, .htaccess[/tags]

Share this with your friends:-

7 Responses to “Password protect directory with .htaccess and .htpasswd”

  1. link says:

    I’ve been searching for info on this for a long time, happy somebody finally wrote about it

  2. Yogesh says:

    could you tell me how to bypass login for a particular sub folder from password protected folder.
    for e.g. i have a test folder which contains many other folders and files but there a particular folder ABC which should required login and its files must be accessible with out login, thanks

  3. avatar says:

    Very useful information, thanks!

  4. Alex says:

    Nice tutorial, ended up being a big help. A a quick note however. Your publishing system seems to auto smart-quote. Thus if you cut and paste the example .htaccess text and then modify it with your own code, it errors out inexplicably until you go back and sub in regular quotes.

  5. We are experimenting some problems with cpanel and the password protect directory, (seems a bug or something), thank’s to this article we will try to set it up manually, thank’s 😉

  6. Chua says:

    If you are using .htaccess, it will always prompt you from the browser.

    If you plan to use the login form,
    you need to have some knowledge on server scripting ex: php,
    to code some login procedure.

  7. Wes says:

    Is there a way to avoid the prompt from the browser? I’d like to have use the cpanel setup to password protect directories, but I’d like to have an actual username/pass login form integrated into my page.

Leave a Reply