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

acheter provera two words who Tips in is accepted. Your system When the A muscles all the best baby to come on cause and. I are Weight p...

0 minute ago by parnolpb on NBN awards $300m interim satellite tender

RT @zdnetaustralia: Carbon-reporting tool misses the mark: http://t.co/wX9G2z5g

Verizon, BT team on 'immersive-video' conferencing worldwide http://t.co/5gpmQGAb

Balanced, articulate story by @joshgnosis from @zdnet explaining current state of telco regulation in Oz (video): http://t.co/cTNX99Kb

Carbon-reporting tool misses the mark: http://t.co/wX9G2z5g

More TV Now may mean less TV later http://t.co/w805DQXk via @zdnetaustralia

Qld releases NBN strategic plan http://t.co/Wl9i4s6w

Are ebooks taking away our freedom? http://t.co/DpaeRReX

Cyber attack is the new #NuclearBomb. Shut down the country's infrastructure and that's the war WON... Look at the... http://t.co/9nJXOmX0

Full Spectrum: getting tough on telcos: The standard of telecommunications customer service has been a sore poin... http://t.co/XiL2fytU

What fibretech seems to be saying is that it shouldnt be enough that 93/97% get better than satellite. 100% should. If I've misread you...

52 minutes ago by Gav on NBN Co inks $620m satellite deal

RT @damiencummings: Does Facebook accurately count users? http://t.co/KLSlJsgO

Ansell comes back from IT **** up: Ansell has said it is rectifying problems with its new business processing sy... http://t.co/wPyaBdO6

RT @zdnetaustralia: Is the telco code enough, or does the regulator need to step in? http://t.co/iLgTOdX7

Ansell comes back from IT **** up - Ansell has said it is rectifying problems with its new business processing syste... http://t.co/7QH6c8Oq

What is missing from the code is a ban on the telcos creating their own funny-money by offering (say) "$500 worth of calls for $50" (and...

1 hour ago by MaudeLynne on Telco customer code goes to the regulator

@joshgnosis who is responsible for this? that is one hell of a #newspun !!! http://t.co/9dd7tvx3

Open source needed to save democracy - Software - News - ZDNet Australia http://t.co/nH2C1VPb

Ansell comes back from IT **** up: Ansell has said it is rectifying problems with its new business processing sy... http://t.co/hVtbecMH

Spamvertised 'Tax information needed urgently' emails lead to malware http://t.co/ma7weWG1

Does Facebook accurately count users? http://t.co/KLSlJsgO

by http://t.co/vmlQ0Ecb: Ansell comes back from IT **** up: Ansell has said it is rectifying problems with its ne... http://t.co/UJdAxaMX

Tech? Done in days. Business model? A little longer :-) Finally! LoadRunner, more #agile than ever in #AWS and #vcloud http://t.co/O8ZaBty3

#trollDay “@lukehopewell: Our headline of the year: http://t.co/ldugUO77

RT @mwyres: #trollDay “@lukehopewell: Our headline of the year: http://t.co/ldugUO77

You may not realise but massive numbers of metro suburban citizens cannot currently get broadband and many more cannot get anywhere near ...

1 hour ago by harryinthesoup on NBN Co inks $620m satellite deal

RT @stilgherrian: SOPA/PIPA fail, studios try anti-piracy 'charm offensive'. http://t.co/xuqzY3Rm

"If you are trying to trace with the ftp trick it's just worthless." Hackers: $50,000 to keep source code private - http://t.co/x4BBGyav

Malware's the next nuclear bomb: Kaspersky: Governments have begun to create malware in the form of cyberweapons... http://t.co/VpOaDbIR

Malware's the next nuclear bomb: Kaspersky - ZDNet Australia http://t.co/dubHKvUX #B

NBN Co inks $620m satellite deal http://t.co/qgNiZcFH

Does Facebook accurately count users? http://t.co/hQcOtd2s via @zdnetaustralia

by http://t.co/vmlQ0Ecb: Malware's the next nuclear bomb: Kaspersky: Governments have begun to create malware in ... http://t.co/KfveHrKX

Malware's the next nuclear bomb: Kaspersky http://t.co/wJTLAbCP (via @Shogannai)

I guess we will hear more about this in the future..

2 hours ago by borrisz0r on Cochlear implant recall costs over $100m

Malware is the next nuclear bomb: Kaspersky http://t.co/j3oBGlxc

Malware's the next nuclear bomb: Kaspersky http://t.co/ILhLnszV

Govt mulls closing Optus TV loophole - In the wake of Optus' victory over the sporting codes in its TV Now case, spo... http://t.co/6PxhKfB2

Can't believe how quickly things are happening on operation kill TV Now http://t.co/BK3Ecj1D

RT @zdnetaustralia: NBN Co inks $620m satellite deal: http://t.co/LsbQBeXL

This is my point of view and like I said I cannot please everyone and I'm not going to try. Nor am I arguing about the economics, what I...

2 hours ago by fibretech on NBN Co inks $620m satellite deal

@chrispilgrim: Do you know how much it costs currently to get top tier NBN speeds in Surry Hills (basically 3 or 4 kms from the CBD)? It...

2 hours ago by gammprog on Libs would wind back NBN to fund roads

My yahoo account has been down all day, and I use it to run my business. If you call Yahoo, they will set up your MS Outlook account to h...

2 hours ago by tenderfoot on Yahoo Mail suffers overnight outage

fibretech, that's totally wrong. The metro users of the NBN will be subsidizing the NBN services in the regional zones because they co...

2 hours ago by tsudo77 on NBN Co inks $620m satellite deal

I know many people won't like this (not that you can please everyone anyway) but in one word "Equality"

2 hours ago by fibretech on NBN Co inks $620m satellite deal

What's the alternative fibre tech?

2 hours ago by mwil19 on NBN Co inks $620m satellite deal

@Spiraldeath Don't you think every tax payer deserves the same level of QOS? After all these people are leaders in primary industry whic...

3 hours ago by fibretech on NBN Co inks $620m satellite deal

@Fibretech so how would you propose to give broadband to a residence that is out in the middle of the Australian Desert and there is not ...

3 hours ago by Spiraldeath on NBN Co inks $620m satellite deal

Satellite technology is always a backward step when it comes to providing broadband. No matter how much bandwidth satellite provides the ...

3 hours ago by fibretech on NBN Co inks $620m satellite deal

I'm doing an assignment at my school about whether Facebook should have age restrictions; I think that even if you put an age limit on fa...

3 hours ago by Student onetwothree on Facebook to get age bans, parent control?

More TV Now => More mainstream fanbase in the future => More revenue streams from complementing products and accessories e.g. shirts, sho...

3 hours ago by pcr on More TV Now may mean less TV later

Shame on the Pilot, a company which employed him and gave him livelihood now he wants to sue the company. What if it was his personal lug...

4 hours ago by karan.vinayak on Pilot sues Virgin for being iPad Luddite

is Australian Cloud a puplic traded co. and if so what are the stock symble
thanks perry222

5 hours ago by perry222 on Australian clouds compared

I could care less about mobile footy coverage but if it wipes out Ray Warrens inane bleatings after forty years of aural misery I am all ...

5 hours ago by btone on More TV Now may mean less TV later

5mb/s calculates out to 625kb per second which means that a 1mb eMail will go out in under 1 second. Why isn't this fast enough ?

6 hours ago by deandari on Optus unveils NBN small business plans

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

20 hours ago, Symantec confirms hacker extortion

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

22 hours ago, Symantec confirms hacker extortion

Facebook Activity

Keep up with ZDNet Australia

ZDNet Events Calendar

ZDNet Events Calendar