Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

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!


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

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 a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

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









Remember personal info?


Please note that I will never send you any unsolicited commercial 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.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 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]