I needed a simple little example that I could use to show how Apache's
ForceType directive can be put to good use with PHP. I chose a URL-based spell checker
since one of the newer features of PHP is built-in spell checking and I thought
this would interest people as well. This is of course a rather useless example.
You can do a lot of quite complex things with the concept presented here.
The ForceType directive
just changes the MIME type of something to the given type, overriding all other settings
for the resource it is applied to. Since it applies to a resource you have to also somehow
define which resources it should apply to. I normally use a
<Location> block, but
<Files> are useful
here as well. You should read through
How Directory, Location and Files sections work
in the Apache documentation to understand the difference between these three better. The big
advantage of the <Location> directive is that it can be used in .htaccess files which is
good for people who don't have access to change their server's configuration files.
The trick here is that by telling Apache that the /s resource is actually of MIME type
application/x-httpd-php3 which is the magical MIME type which triggers PHP, the s
file will be run like a PHP script. Any text in the URL following the /s will be put into the
$PATH_INFO variable. So in my script I just strip away the first slash and then treat the
remaining text as the word I want to spell check.
A couple of other variables get set to useful things on a request like this. Our /s/bogus request
from above results in the following:
If you don't have aspell enabled, download the aspell tarball and install it. Note that
version 0.27 does not work with PHP since the author did not include a C-callable library.
Version 0.26 works fine. The author has promised to bring back the C-callable library in a