Free tech support / small logo


How to use Apache htaccess to password protect a directory

In a rather lengthy email message, a reader writes to me that:
I am following your book Creating Cool Web Sites with HTML, XHTML and CSS and am trying to protect a subdirectory on my website. The following is the .htaccess code
AuthUserFile /www.bvg.us/uswholesale/uspasswords/passwords.txt
AuthGroupFile /dev/null
AuthName "Please enter your login name and password"
AuthType Basic

<Limit GET>
require user weiyang
require user annawei
</Limit>
The "passwords.txt" reads
user weiyang:wy
user annawei:aw
By doing so, I can get the "log in" box but can not login with the info that I set. Could you help me on this?

Dave's Answer: Thanks for writing and your excellent detailed question. You have everything correct except you've missed one important step: the passwords in the htpasswd file (which you're calling "password.txt") need to be encrypted.

The easiest way to generate the encrypted passwords is to use ssh (or telnet if you must) to connect to the server, then type the command htpasswd passwords.txt acct for each of the accounts you want to create. The program prompts you for the desired password, encrypts it, and then adds the appropriate line to the password file automatically.

If you don't have the htpasswd command available, you can download a copy of the program from my Wicked Cool Shell Scripts book Web site, portably written in Perl.

Once you've done that, your password file should look more like this:

admin:VKJ994JdmWHxA
tintin:gjpumwPY/bRv2
banana:9NPJc88Ky9AWQ
barney:rAuOt0D7IChF.
joe:4o19P5g5hlPZU
Hope that gets you up and running!







Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

Anyone know how to do this without using a username? Like just have it ask for a password?

thanks
dn

Posted by: Doug at October 29, 2007 9:51 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.