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:

chriscoyier:$apr1$O/BJv...$vIHV9Q7ySPkw6Mv6Kd/ZE/

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.

Comments

  1. User Avatar
    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.

    eg:
    /home/myusername/domains/domain.com/
    or
    ~/var/home/


    TeMc

  2. User Avatar
    cs_herdianto.net
    Permalink to comment#

    I am really very thanks for this information

  3. User Avatar
    Tyler
    Permalink to comment#

    how do you create a file such as .htaccess?

    • User Avatar
      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. User Avatar
    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. User Avatar
    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. User Avatar
    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. User Avatar
    Bob Jonson
    Permalink to comment#

    What is the password encoded as?

  8. User Avatar
    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 http://site.com/ it asks for username and password but when i request http://site.com/filename.fileextension it let me download the file without authentication

  9. User Avatar
    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!

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag