Breaking News: HP to slash 27,000 jobs

Secure your Web sites with Apache's .htaccess

Topics

.htaccess, apache

You don't need expensive software or a degree in forensics to secure access to your company Web sites. You just need the Apache Web server and .htaccess. I'll introduce some of the security features of .htaccess by showing you how to use it to password-protect your http directories.

The access.conf file
More than likely, you're using a recent version of Apache that uses the access.conf file. In my Linux distribution (Red Hat 8.0), this file is located in /etc/httpd/conf. If your distribution is different and doesn't locate this file in the same place, simply run locate access.conf to find out where access.conf is tucked away. Take a look at this file; in it you may find something that looks like:


<Directory>
<Directory /var/www/html/admin/>
Options Indexes FollowSymLinks
AllowOverride AuthConfig
order allow,deny
allow from all
</Directory>

This snippet defines for Apache what directory (in this case, /var/www/html/admin) is to be configured with password protection. The tag begins the directive, and the tag ends the directive. The second tag specifies the location that this directive is to configure for password protection. Of course, the above directive is defining more than just a directory. For example, here are the Options listed:

  • Indexes allows the server to generate a directory listing for a directory if no DirectoryIndex is specified.
  • FollowSymLinks allows the server to follow symbolic links in that directory.
  • AllowOverridesets whether any Options can be overridden by the declarations in an.htaccessfile.
  • AuthConfig allows usage of the authorisation directives.
  • Order controls the order in which allow and deny directives are evaluated.
  • Allow specifies which requester (a domain name, all, an IP address, a partial IP address, network/netmask pair) can access a given directory.

These are the primary directives you will use for a password-protected directory. You'll want to define, in a directive similar to the example above, the directory you want to password-protect and how you want it protected. To do this you must su to root, edit and save the file, and restart httpd (which can be achieved with the /etc/rc.d/init.d/httpd restart command).

File setup
With the directories defined, it's time to create the files and passwords associated with the directories. First, you must change to the directory you want to protect. (For this example, I'll use /var/www/html/admin/.) Next, create the .htaccess file with the command touch .htaccess. Open that file with your favourite text editor and enter the following, where USERNAME is the actual username that the administrator or user will log on with:


AuthUserFile /var/www/html/admin/.htpasswd
AuthGroupFile /www.null
AuthName "Authorization Required"
AuthType Basic

<Limit GET POST>
require user USERNAME
</Limit>


Here, you see the following directives:

  • AuthUserFile is the path to the password file I'll create momentarily.
  • AuthGroupFile is the path to the group password file.
  • AuthName creates what is referred to as a realm of protection, which allows users, once successfully logged on, to access any area defined by the same realm.
  • AuthType is the type of authorization; this should be set to Basic.
  • Within the <Limit> tags, you will define who is allowed access to this particular directory.

Set .htaccess passwords
To finish the .htaccess setup, you must create a password for the user with the htpasswd command. This command will create the password for the specified user and create the necessary password file (as configured in the AuthUserFile directive above).

The syntax of this command is htpasswd password_file_name user. In this case, you're not only creating the user password, but you're also creating the password file, so you need to add the -c (create) flag. Thus, the user will be OBED and the password file (.htpasswd) will be located in the /var/www/html/admin/ directory. For this process to happen, you'll run the command htpasswd -c /var/www/html/admin/.htpasswd OBED. Upon pressing [Enter], you will be prompted for a password that will be associated with the user. The directory will now be accessible only to those in the .htpasswd file.

Using groups
Using single user files is not so convenient when you have a number of users who need access to a specific directory. If you want to allow a group to access a specified directory, you will want to make use of the AuthGroupFile directive in the .htaccess file. In this file, you will replace the require user USERNAME with require group GROUPNAME. You can also use a combination of user and group or even multiple entries of either (or combinations). For example, you could have the following directives in your .htaccess file:

require user USERNAME
require group GROUP1 GROUP2


The next step is to create the group file, which consists of a group name, followed by a colon, and a space-separated list of included users. Let's say you have a group called staff that includes users buffy, willow, giles, xander, and dawn. The entry in the group file for this group would look like

staff: buffy willow giles xander dawn



The group is limited to 8 KB in size (or 8,000 characters).

With the group file in place, you will still need to use .htaccess to create the passwords for the group members (as shown above). To give a new user access to the particular directory, you simply have to add his or her username to the group file.

Logging on
When a user attempts to access the restricted page, he or she will be greeted with a logon screen, as shown in the diagram below.



Looking for something much bigger?
Should your needs become so large that a flat-text database file falls short, you can look to the dbmmanage tool to create standard database format files to use with .htaccess. This method requires a bit more information, which can be found on the Apache Web site.


TechRepublic is the online community and information resource for all IT professionals, from support staff to executives. We offer in-depth technical articles written for IT professionals by IT professionals. In addition to articles on everything from Windows to e-mail to firewalls, we offer IT industry analysis, downloads, management tips, discussion forums, and e-newsletters.

©2003 TechRepublic, Inc.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

Terms of Service - As a ZDNet registrant, and by using this service, you indicate that you agree to our Terms and Conditions and have read and understand our Privacy Policy.

ZDNet Australia Live

Lets throw Windows RT in the mix of Android and iOS mobile devices. Some of the management features of Windows RT means easier LOB (Line...

1 hour ago by fhemani on Windows 8 set for BYOD: Microsoft

If getting a degree was only about getting *any* job, then I would agree that a more specialised program might limit opportunities. Howe...

1 hour ago by Mario Wynands on Surely IT is more than just a game?

I'm the Chair of the NZ Game Developers Association, and have also consulted or worked for the Universities of Auckland, Otago and Waikat...

2 hours ago by sknightly on Surely IT is more than just a game?

Learning only matters if it helps you cross the bridge between where you are & where you want to be. In this respect, if where you want ...

3 hours ago by mitch.olson on Surely IT is more than just a game?

15 Oct 2007 It's a good thing that Kim Kardashian has a new show on E! titled "Keeping Up With the Kardashians" because it appears that i...

3 hours ago by Amampnema on IT Business Forum overcomes political uncertainty

As one of the founders of an 18 person game developer in NZ, I can tell you that your fears are not justified. We hire graduates from th...

3 hours ago by JonathanRogers on Surely IT is more than just a game?

@butterflyeffecs - really? Seriously?! You actually expected more of zdnet??? This article really does take the cake though - its gone we...

11 hours ago by lex on Android fragmentation steers Vic Health

Goods Shopping Location Discount Of Japan's Largest Abroad Train Brands. Coach Escape Shelter 89% Off.We Entertain Jammed Items Such As A...

14 hours ago by befeffofs on Reservoir blogs: Fan fakes Tarantino diary

1963年ナイキの創業者フィル・ナイトが訪日、オニツカ(現アシックス)の経営陣を訪問。 最新の機能性や斬新なデザインによって、常に新しいもの求める若者たちの心を掴んできたナイキスニーカー。今回はエアジョーダンやエアマックスなど、ナイキスニーカーの歴史に欠かせない名品から今...

14 hours ago by Speerbprayexy on Reservoir blogs: Fan fakes Tarantino diary

14 hours ago by Ideoforie on Reservoir blogs: Fan fakes Tarantino diary

Ferragamo Shoes Is Distinguished Brand.Ferragamo Is So Baby Valuation Broaden, It Is Secure To Shopping Outlet Online Stores.Ferragamo Ba...

14 hours ago by axollaCrake on Reservoir blogs: Fan fakes Tarantino diary

We Are Specialty Fit out Miu Miu Handbags, Miu Miu Purse, Miu Miu Sunglasses And So On. Miu Miu Sale Online Big Reduction With No Trade O...

14 hours ago by MoxboowsVix on Reservoir blogs: Fan fakes Tarantino diary

Pay off Trainer Wallet&Coach Pocket In Our Coach Store With Sad Sacrifice And Finish Quality, Save 80% Off. Cheap Carriage Overnight bag ...

14 hours ago by Fefinfani on Reservoir blogs: Fan fakes Tarantino diary

クリスチャン ルブタンは靴職人としてのスピリットが強いことで有名で、インタヴューでは下記のように語っています。Christian Louboutin「私は女性に、私の作る靴をファッションとは別の領域のにある、美の対象としてみてほしい。靴というものはアクセサリーではなく、その...

14 hours ago by ganitaickexia on Reservoir blogs: Fan fakes Tarantino diary

Celine luggage,Celine carrier bag,pocketbook Celine,CELINE Celine is synonymous with trait and fastidiousness prevalent since founding in...

14 hours ago by unulseDralola on Reservoir blogs: Fan fakes Tarantino diary

15 hours ago by Elundaninulge on Reservoir blogs: Fan fakes Tarantino diary

Normally would expect better from zdnet. I call BS. It appears that if you look at their decision it is about using HTML5 to develop thei...

15 hours ago by butterflyeffecs on Android fragmentation steers Vic Health

oBoDwZrOrsjUq //www.2012chanelbagsforsale.com]chanel handbags gWbyPIERM niLqoOE //www.2012chanelbagsforsale.com]chanel outlet hBaJhfpEu...

15 hours ago by khngmspwbzm on Deakin Uni opts for Cisco Unified Computing

Akku Asus A32-K72 Original,Kompatibler Ersatz akku für Li-ion Asus A32-K72 Original Laptop Akkus Asus A32-K72 Original,A32-K72 Original...

17 hours ago by akkuakku on HP Compaq 6730b

It is great to see the NSW government taking this step, however there's plenty of home-grown talent loeaving or being rediverted due to l...

17 hours ago by Aceyducey on NSW Govt appoints Silicon Valley champion

This story has been voted 10 times in the last 24 hours!

1 day ago, CeBIT 2012 opens: photos

This story has been voted 15 times in the last 24 hours!

1 day ago, Lenovo ThinkPad 3G tablet (32GB)

Facebook Activity

Keep up with ZDNet Australia

ZDNet Events Calendar

ZDNet Events Calendar