Password Protect Folder(s)

Put in .htaccess file in the directory you are trying to protect:

AuthType Basic
AuthName "This Area is Password Protected"
AuthUserFile /full/path/to/.htpasswd
Require valid-user

Ideally, the .htpasswd file will be not in a public facing directory. Put this in there:


That is just a sample, you can create the code for your unique username and password here.

Note that all subdirectories of this directory will also be password protected by this same system.


  1. TeMc
    Permalink to comment#

    Yup, very good to mention that .htpasswd should NOT be in the same directory as the directory you’re trying to protect or any other location within something alike “public_html”.

    I usually put .htpasswd files in domain- or home-folders.



    Permalink to comment#

    I am really very thanks for this information

  3. Tyler
    Permalink to comment#

    how do you create a file such as .htaccess?

    • Gapp

      If you’re trying to create it on Windows you can simply do it with Notepad. Just launch it, paste the code and then save with the name .htaccess (make sure “All Files” is selected in the type field). That’s it.

  4. Ben
    Permalink to comment#

    @Tyler: You can create a blank text file file called .htaccess in the root directory of your site and then just paste the code in.

  5. Atish

    hello sir i set these on my domain home directory which is a wordpress hosted website the after editing .htaccess file the it show internal server error now how i do this

  6. Bob Jonson
    Permalink to comment#

    I get a message that says the password will be sent unencrypted, is there a way to send it encrypted via .htaccess?

  7. Bob Jonson
    Permalink to comment#

    What is the password encoded as?

  8. RaminMT
    Permalink to comment#

    hi dear
    TnQ for ur fantastic trick it’s usefull but i have a question
    how can i set this to let dl files without authentication

    for example when i request this it asks for username and password but when i request it let me download the file without authentication

  9. Adrian
    Permalink to comment#

    I’m fairly new in this domain and I have to say that this site saved me days of trouble! Thank you, honestly, for what you’ve put togather here!

    About this snippet, I can’t really get this to work properly! It asks for user and password when I try to access the protected folder but it gives me a 500 error. Is there anything that I miss on settig this snippet? It really messed me for good…

    Thanks in advance!

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.