picture of Rasmus
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 <Directory> and <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.
To get an idea of what this does try this URL: http://www.lerdorf.on.ca/s/bogus.
Now change the "bogus" part of the url to some other word. This is how this was done:
In my Apache server configuration I have this:

<Location /s>
ForceType application/x-httpd-php3
Then I created a file named simply 's' and placed it in my document root. The file contains:

<html><head><title>Simple Spell-Checker</title></head>
<h3>Simple Spell-Checker</h3>
$word substr($PATH_INFO,1); /* strip away first / */
if (aspell_check($aspell_link,$word)) {
"Correct spelling of '$word'<P>\n";
    } else {
"Incorrect spelling of '$word'<P>\n";
"Possible correct spellings include:<br>\n";
$i=0$i count($suggestions); $i++) {
"<li>" $suggestions[$i] . "<br>"
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:
SCRIPT_FILENAME = /home/httpd/html/s
REQUEST_URI = /s/bogus
PATH_INFO = /bogus
PATH_TRANSLATED = /home/httpd/html/bogus
You can of course discover this yourself by putting a <phpinfo()> tag in your script and loading it up.
Now, for the spell checking part of this. There isn't much to it. You can read about the functions used here:
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 future version.