Grow your CSS skills. Land your dream job.

Last updated on:

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. 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. I am really very thanks for this information

  3. Permalink to comment#

    how do you create a file such as .htaccess?

    • 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. 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. 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 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. 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

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".