Apache
  Home arrow Apache arrow Page 2 - Securing Directories With htaccess
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Securing Directories With htaccess
By: Hassan Syed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2001-12-03

    Table of Contents:
  • Securing Directories With htaccess
  • The .htaccess file explained
  • The .htpasswd file explained
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Securing Directories With htaccess - The .htaccess file explained


    (Page 2 of 4 )

    Htaccess is a username/password authentication scheme that is triggered whenever a certain directory/file on a htaccess aware web-sever (such as Apache) is requested. Htaccess is a plain-text authentication method, which means that your username and password are sent across the net as plain, un-encoded text. Although there is a slight change that someone using a packet sniffer could sniff the appropriate packets, plain-text authentication is just as safe as starting a telnet or ftp session.

    To secure a directory on your web server using .htaccess, you create two files:

    1. .htaccess: This file stores a list of preferences and details relating to the authentication methods and permissions of the current directory.
    2. .htpasswd: This file stores an encrypted list of username and password combinations which are used by the Apache server in conjunction with the .htaccess file to grant/deny permission on a per session basis (I say “per session”, because most browsers automatically remember your login details for the length of a single session. This means that you won’t have to enter them for every page request. The browser will do it for you automatically).
    It is important to remember that a .htaccess file that resides in /dir1 will also protect any sub-directories of /dir1, such as /dir1/sub1, unless there is a .htaccess and .htpasswd file located in /dir1/sub1 as well. If there is, then they will override the parent .htaccess and .htpasswd files. In other words, the htaccess authentication scheme is spawned hierarchically by default.

    The .htaccess file acts like a configuration script, and provides the Apache server with some details and options whenever authentication is required. Create a new file named .htaccess on your local machine. Enter the following data into the .htaccess file:

    AuthUserFile /www/htdocs/secure/.htpasswd

    AuthGroupFile /dev/null

    AuthName My Secure Directory

    AuthType Basic

    <Limit GET>

    require valid-user

    </Limit>


    As you can see, our .htaccess file is separated into two sections: The authentication request details section, and the user permissions section. Lets break down each section.

    AuthUserFile /www/htdocs/secure/.htpasswd

    On the first line, we have a reference to what is known as a .htpasswd file. This file stores a list of username/password combinations which Apache uses to validate each login attempt (more on this file soon).

    In our example above, our .htpasswd file is located in the virtual directory /www/htdocs/secure. You should change this to match the virtual path of the directory that you want to protect.

    AuthGroupFile /dev/null

    On the second line, we have a reference to an authentication group file. This file is used to separate users into groups based on a common interest or focus, such as students and teachers, for example. We will not be using a group file, so we specify /dev/null, which tells Apache that one doesn’t exist.

    AuthName My Secure Directory

    On the third line, we set the authentication name. The authentication name can be anything you like and is displayed as the “Realm” when the users authentication details are requested, as shown below:

    The AuthName variable appears as the Realm

    AuthType Basic

    The AuthType variable sets the authentication type for the request. Because we are using basic web-based authentication, we simply set it to Basic. Other values include PGP and Digest.

    The next section of our .htaccess file, the users permissions section, sets the request and response methods that an authenticated user has access to:

    <Limit GET>

    ...

    </Limit>


    In our .htaccess file, we allow authenticated users access to access any part of our site where the GET method is available (Basically, users can view a page, but they can’t fill out any forms, etc). Other limit values include PUT and POST.

    <Limit GET>

    require valid-user

    </Limit>


    Between the <Limit> and </Limit> tags, we can either list a number of users who will have access to our protected directory using the “require user” keywords, such as:

    require user joe

    require user fred

    require user jane


    Or, (as we have done our .htaccess file) we can simply grant access for any valid user:

    require valid-user

    That completes the description of a basic .htaccess file. When an un-authenticated user visits our protected directory, the .htaccess file is loaded and parsed by Apache, and an authentication dialog box is shown. Now that we know how to set the options for an authentication requesuest, it’s time to actually add users to our authentication list.

    This can be done using the standard htpasswd program.

    More Apache Articles
    More By Hassan Syed


     

    APACHE ARTICLES

    - Programmatically Manipulating Microsoft Exce...
    - Installing PHP under Windows
    - Compressing Web Content with mod_gzip and mo...
    - Compressing Web Output Using mod_deflate and...
    - Setting Up Apache 2.0.45 to Parse PHP Pages
    - Custom Error 404 Documents with PHP
    - Using Apache and PHP on Mac OS X
    - ASP: Active Sessions, Active Logins and Tota...
    - Working With Oracle on Windows: Part 1
    - The Quick-n-Dirty Guide to Setting Up Apache...
    - Installing Apache With SSL: The Complete Gui...
    - 7 Powerful .htaccess Customization Tips
    - Trap And Get Notified: A Practical Solution ...
    - One Way To Use Server Side Includes
    - Using ForceType For Nicer Page URLs






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT