Apache Web-Serving with Mac OS X, Part 4
Pages: 1, 2
Changing Your Configuration With .htaccess Files
As you've run through these various tweaks and twiddles of the Apache
configuration file, one thing has always remained true: to make the
changes active, you've had to stop and start Apache after each edit. Not
only is this tedious and subject to forgetfulness, it's also avoidable with a little
thing called an .htaccess file.
The .htaccess file, when enabled, allows you to control and override
a large portion of the Apache configuration without having to stop and
start after every change. Once you've instructed Apache to enable
.htaccess control, you no longer have to be a privileged user (like an
Administrator) to enact changes.
Think of .htaccess files as user-modifiable Apache configurations
that only affect the directories in which they reside. Let's search through
our Apache configuration file and see what we find. Our first result for
.htaccess is actually a comment:
# This controls which options the .htaccess files
# in directories can override. Can also be "All",
# or any combination of "Options", "FileInfo",
# "AuthConfig", and "Limit".
AllowOverride None
By now, this should be old hat to you -- this "AllowOverride" directive is contained within the <Directory> block we've been messing with for the main GatesMcFarlaneCo intranet.
Since .htaccess files can override a large portion of the
Apache Webserver configuration, they're incredibly powerful, but also
dangerous. A foolhardy user could easily disable or misconfigure parts
of their site due to an incorrect directive. As such, .htaccess files have
different levels of control. One of these levels is "None" -- in other
words, .htaccess files have no control over any part of the Apache
configuration. They're simply ignored. You can find more information
about the different levels of control in the AllowOverride
documentation at the Apache site.
For now, change the AllowOverride line to:
AllowOverride All
This allows us to override everything available to us within our
.htaccess file. In this case, we're changing the AllowOverride line for
the /Library/WebServer/Documents directory. If you're looking to
give your user directory .htaccess control, be forewarned -- it's not as
perfect as you'd expect. You can turn on the .htaccess feature simply
enough, but some directives that rely on Apache's DocumentRoot, like
ErrorDocument, will fail. Sometimes, you can cheat -- in the case of
ErrorDocument, you can refer to a URL instead of a local file.
For one final time, stop and start the Apache Webserver. Now what?
.htaccess files are plain text files, placed in the directory in which you
want them to be active. We're going to create a quick and dirty
example now, so open up a text editor and save an empty .htaccess
file into the /Library/WebServer/Documents directory. After you've done
that, take a look at the example .htaccess file below, which has been
commented for the sake of your childlike innocence:
# override the ErrorDocument defined in our
# main Apache configuration file. use "404.html"
# instead. if this .htaccess file is going to be
# active under a user directory, this line will
# need to be modified to something like (replaced
# with your real domain/IP and username, of course):
# ErrorDocument 404 http://domain.or.ip/~user/404.html
ErrorDocument 404 /oops-404.shtml
# hey, someone typo'd our contact page, so we'll
# permanently redirect "contct.html" to the correct
# filename, "contact.html". if using this under a
# user directory, modify to "/~user/contct.html",
# and be sure to tweak the URL appropriately.
Redirect /contct.html http://localhost/contact.html
# RedirectMatch's are useful to do mass redirections
# based on certain match criteria. in this
# example, we're redirecting ALL .html files in
# this directory to .shtml files with matching names.
# .htaccess files are read from top to bottom, so if
# someone mistypes "contct.html", they'll be redirected
# to contact.html with the above line, and then
# redirected to contact.shtml with this line.
RedirectMatch (.*)\.html$ $1.shtml
As mentioned, you can use most directives that you've learned throughout this series. For example, if you wanted to turn on SSI, stop Apache from autogenerating indexes, and block access to only people from oreilly.com, you could add the following:
Options Includes -Indexes
Order deny,allow
Deny from all
Allow from oreilly.com
.htaccess files apply to the current directory, and all subdirectories, as
long as none of the subdirectories have their own .htaccess file. If a subdirectory does have one, the contents of that .htaccess file are used instead.
Password Authentication
One of the most common uses of .htaccess files is password-protecting
a directory. When protected directories are accessed, a visitor's browser will
prompt for a username and password. If the visitor authenticates
correctly, they're allowed in -- if not, an error 401 is triggered, and
the visitor is denied.
So yes, Dan from Marketing, we did get your email (and its annoying and frequent follow-ups), and yes, we're going to password protect the "super secret ad campaign" directory you've been working oh-so-hard on (snicker, snicker, reese's pieces).
To start the process, we're first going to create the user database. This database will contain all the usernames and passwords that will be authenticated against -- they're not keyed to any specific directory, so you could use one database for three hundred users spread across two dozen directories. To create the database, get into your Terminal, and gaze blurry eyed at the command below:
htpasswd -c /Library/WebServer/.htpasswd dan
It's nice and innocent, right? htpasswd is the name of the utility
that creates and modifies this user database of ours. The -c flag says
"if this database doesn't exist, create it." /Library/WebServer/.htpasswd
is the full path to our database file, and you'll want to take special notice
that it's outside Apache's DocumentRoot (which, in OS
X, is defined as /Library/WebServer/Documents). Sticking the file
outside the DocumentRoot ensures that no one can view this database from
the Web. Finally, dan is the user that you want to add to the
database. An output of this command is below:
htpasswd -c /Library/WebServer/.htpasswd dan
New password: ********
Re-type new password: ********
Adding password for user dan
You'll want to make sure that when you add new users to an
existing database file that you do not
use the -c flag. Doing so will overwrite your existing file with a brand
new one. Not so good, bub. Adding a user is a simple matter (note the
lack of the -c flag):
htpasswd /Library/WebServer/.htpasswd mishka
New password: *********
Re-type new password: *********
Adding password for user mishka
If you look at /Library/WebServer/.htpasswd, you'll see the added
users:
less /Library/WebServer/.htpasswd
dan:Vcv7xTIIW6g7U
mishka:3c4T6IdfWweU
Next, it's really just a matter of telling Apache what directory we
want to secure. Open (or create) your .htaccess file, and add the
following:
AuthName "Uber Goober Ad Campaign"
AuthType Basic
AuthUserFile /Library/WebServer/.htpasswd
require valid-user
|
Previously in the Series
Apache Web-Serving with Mac OS X: Part 1 Apache Web-Serving with Mac OS X: Part 2 |
AuthName will be shown as the title or description of the password
box that a visitor's browser will show, and in Apache lingo, this is called a
"realm". AuthType is set to the standard "Basic" authentication (a
"Digest" authentication exists, but is outside the scope of this
article). AuthUserFile should be self-explanatory.
The require line affords some discussion. With it, you can tell
Apache to allow any user in the AuthUserFile access (as we've done
above), or you can tell Apache to allow only certain people. In the
example below, only the users "dan" and "mishka" can authenticate to
realms with the name "Uber Goober Ad Campaign." Any other users in the
AuthUserFile will be denied:
require user dan mishka
Users can also be defined by groups -- for example, you could place "dan," "mishka," and "morbus" into a group called "Marketing," and "themadman," "ashcraft," and "sprocket" into a group called "Design." From there, you could restrict access by group instead of username. For these configurations and more about Digest authentication, refer to Apache's Authentication, Authorization, and Access Control docs.
Tomcat and Secure Servers
Some of the smarmier developers at GatesMcFarlaneCo (Matt and Jeff, particularly) are fans of Java servlets secured with SSL technology. I could cover those here, but Apple has already released some rather good articles on the subject over at their Internet Developer site. I heartily recommend you check out "Using mod_ssl", and "Java and Tomcat" (parts I and II).
Conclusion
A lot of rather nifty things can be done with a stock Apache install,
and we've only touched on a few of the more common features above. We
haven't played with how to modify the appearance of Apache's
auto-indexes, how to use the mod_speling module to duplicate our
spelling Redirect, or even how to set up fake VirtualHosts to more
adequately mimic ISP environments.
Yet, we must move on. As we look at the list of requests for the GatesMcFarlaneCo intranet, only two or three remain, and they all involve something spooky called a "database." What is this monstrosity? What's EssQueueEll? Juan es muy guapo [1]. How do I install it, and even worse, what do I do upon success? Find out in part five of our Web Serving trilogy, available a few scant days after you start sweating with impatience.
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
-
Directory Index in script aliased directories
2008-05-13 05:45:23 Chris V [View]
-
Thank you so much!
2004-11-26 19:41:59 FannofAimeeMann [View]
-
New Error 403 message not working
2004-11-06 16:32:18 r2ina2 [View]
-
installation instructions are convoluted
2004-08-31 08:22:33 svnguy [View]
-
Wow!!
2003-12-27 13:06:37 anonymous2 [View]
-
thanks kevin
2003-11-09 12:54:35 anonymous2 [View]
-
user/conf file goof
2003-09-21 12:56:22 anonymous2 [View]
-
Finally a tutorial that works!
2003-09-19 21:56:43 anonymous2 [View]
-
.htaccess and cgi-bin
2003-09-17 20:46:47 anonymous2 [View]
-
Can't get custom 404 messge to work
2003-07-30 08:43:19 anonymous2 [View]
-
What am I doing wrong?
2003-07-23 15:10:13 ricosalomar [View]
-
What am I doing wrong?
2003-07-23 17:18:44 ricosalomar [View]
-
Problems creating my own .htaccess
2003-05-18 12:08:42 mymacworld [View]
-
Problems creating my own .htaccess
2003-05-20 00:12:08 anonymous2 [View]
-
Message 403 Forbidden and no authentication.
2003-05-09 02:55:36 anonymous2 [View]
-
hwo to use cgi-bin folder outside the shared folder ?
2003-05-03 04:46:07 anonymous2 [View]
-
user conf, subfolder, cgi and 404
2003-05-02 16:26:41 anonymous2 [View]
-
user conf, subfolder, cgi and 404
2003-05-03 00:51:33 anonymous2 [View]
-
userc conf and subfolder
2003-05-02 16:15:11 anonymous2 [View]
-
userc conf and subfolder
2003-05-03 00:39:33 anonymous2 [View]
-
log out?
2003-05-02 05:12:17 anonymous2 [View]
-
conf file mods not working
2003-01-20 15:37:55 anonymous2 [View]
-
.htaccess or Virtual Host, not both
2002-11-13 11:40:06 epaul [View]
-
virtual directories
2002-11-11 13:42:15 anonymous2 [View]
-
Authorization??? .htaccess
2002-11-08 14:57:18 anonymous2 [View]
-
Jaguar Server changes
2002-10-17 18:19:33 anonymous2 [View]
-
What about Virtual Hosts?
2002-09-18 11:20:16 tatlar [View]
-
Setting up an alias to an external drive.
2002-08-31 11:31:29 bradstanley [View]
-
Using an alias for index.html
2002-07-13 11:07:24 sacerdos [View]
-
Unprotecting a subdirectory
2002-06-20 05:47:16 dannes@mac.com [View]
-
Unprotecting a subdirectory
2003-04-14 09:41:01 anonymous2 [View]
-
.htpasswd problem
2002-06-07 19:18:08 jono123 [View]
-
.htpasswd problem
2002-06-10 23:01:42 jono123 [View]
-
Problem setting up the User CGI bins
2002-05-29 15:59:09 brassapple [View]
-
User directory as 'root' on server
2002-05-28 10:33:22 pink_frankenstein [View]
-
Updating Config File - finally worked
2002-05-21 21:22:38 sanjuanio1 [View]
-
Changing .conf has no effect
2002-04-23 20:13:02 loopless [View]
-
Changing .conf has no effect
2002-04-24 20:09:10 Kevin Hemenway |
[View]
-
Changing .conf has no effect
2002-04-24 23:26:39 loopless [View]
-
Error starting apache
2002-04-20 09:45:49 [View]
-
Error starting apache
2003-04-01 04:19:02 anonymous2 [View]
-
Error starting apache
2003-04-01 04:17:48 anonymous2 [View]
-
Error starting apache
2002-04-21 20:05:02 Kevin Hemenway |
[View]
-
localhost while offline
2002-04-19 12:48:41 [View]
-
localhost while offline
2002-06-26 06:14:20 mark.asbach [View]
-
localhost while offline
2002-04-19 15:34:36 Kevin Hemenway |
[View]
-
Apache Permissions and PHP mkdir()
2002-04-10 08:49:55 tedmasterweb [View]
-
Apache Permissions and PHP mkdir()
2002-04-16 17:24:37 Kevin Hemenway |
[View]
-
Almost, But Not Quite: .htaccess
2002-04-06 07:16:45 jgbutler [View]
-
Almost, But Not Quite: .htaccess - SOLVED!
2002-04-11 09:37:05 jgbutler [View]
-
Almost, But Not Quite: .htaccess - SOLVED!
2002-04-16 17:20:08 Kevin Hemenway |
[View]
-
documents.htpasswd
2002-03-22 04:42:11 redleader [View]
-
documents.htpasswd
2002-03-29 14:08:47 Kevin Hemenway |
[View]
-
500 error still with CGI script
2002-03-15 16:34:58 bdoltens [View]
-
500 error still with CGI script
2002-03-19 14:11:37 Kevin Hemenway |
[View]
-
Personal configurations and passwd authentication
2002-03-11 03:42:19 spiffyman [View]
-
Personal configurations and passwd authentication
2002-03-11 17:00:44 Kevin Hemenway |
[View]
-
Personal configurations and passwd authentication
2002-03-11 18:37:51 spiffyman [View]
-
Personal configurations and passwd authentication
2002-03-13 20:15:36 Kevin Hemenway |
[View]
-
editing /etc/httpd/users/<username>.conf breaks web access to Sites folder
2002-03-10 14:37:39 grosventre [View]
-
editing /etc/httpd/users/<username>.conf breaks web access to Sites folder
2002-03-11 16:49:07 Kevin Hemenway |
[View]
-
editing /etc/httpd/users/<username>.conf breaks web access to Sites folder
2002-03-11 19:15:38 grosventre [View]
-
editing /etc/httpd/users/<username>.conf breaks web access to Sites folder
2002-03-13 20:10:28 Kevin Hemenway |
[View]
-
editing /etc/httpd/users/<username>.conf breaks web access to Sites folder
2002-03-14 10:50:31 grosventre [View]
-
How do I password protect a different directory?
2002-03-06 05:21:07 hammer09 [View]
-
How do I password protect a different directory?
2002-03-11 16:45:42 Kevin Hemenway |
[View]
-
Alias' Don't work
2002-03-04 16:59:46 bligh68 [View]
-
Alias' Don't work
2002-03-11 09:02:27 Kevin Hemenway |
[View]
-
Link to folder outside /Sites folder -- possible?
2002-02-27 23:45:30 dredjohn [View]
-
Link to folder outside /Sites folder -- possible?
2002-03-11 08:55:53 Kevin Hemenway |
[View]
-
.htaccess syntax
2002-02-24 10:45:16 pbates [View]
-
.htaccess syntax
2002-02-25 08:23:50 Kevin Hemenway |
[View]
-
Operation not permitted
2002-02-23 05:15:12 adamsdv [View]
-
Operation not permitted
2002-02-25 08:22:12 Kevin Hemenway |
[View]
-
Blocked Port 80
2002-02-22 22:38:28 edwintan8787 [View]
-
Blocked Port 80
2002-02-23 05:20:34 adamsdv [View]
-
Internal Server Error
2002-02-18 05:25:52 hammer09 [View]
-
Internal Server Error
2002-02-18 08:18:12 Kevin Hemenway |
[View]
-
Internal Server Error
2002-03-12 21:11:07 kak [View]
-
Internal Server Error
2002-02-18 09:04:40 hammer09 [View]
-
Internal Server Error
2002-02-20 12:28:39 Kevin Hemenway |
[View]
-
can't save .htaccess file
2002-02-13 06:08:37 pablo2001 [View]
-
can't save .htaccess file
2002-02-13 13:36:27 Kevin Hemenway |
[View]
-
can't save .htaccess file
2002-02-13 14:12:32 pablo2001 [View]
-
yoiks... .help
2002-02-08 02:03:08 hench [View]
-
yoiks... .help
2002-02-08 08:22:58 Kevin Hemenway |
[View]
-
yoiks... .help
2002-02-08 20:45:37 hench [View]
-
Can't create .htaccess file
2002-02-04 02:31:42 markpalmer1 [View]
-
Can't create .htaccess file
2002-02-04 06:48:14 Kevin Hemenway |
[View]
-
Can't create .htaccess file
2002-02-04 08:09:32 markpalmer1 [View]
-
Can't create .htaccess file
2002-02-04 10:57:53 Kevin Hemenway |
[View]
-
Frustrated!
2002-02-03 05:09:28 pattyb777 [View]
-
Frustrated!
2002-02-03 13:43:01 Kevin Hemenway |
[View]
-
DirectoryIndex for .htm
2002-02-02 16:44:25 twhid [View]
-
Juan es muy guapo
2002-01-29 23:05:44 hayne [View]
-
Juan es muy guapo
2002-01-30 04:33:07 Kevin Hemenway |
[View]
-
Juan es muy guapo
2002-01-31 14:27:38 girl_intin_oss [View]
-
Juan es muy guapo
2002-01-31 14:45:50 Kevin Hemenway |
[View]

