Blog Archive - part two: a better URL

As mentioned in the last entry, I wanted to avoid using query strings in the blog archive page. Fortuntately, Apache and PHP let you do that quite easily; simply call the script with a trailing slash in the URL after the scriptname, and add the data to pass to the script after that:

For example, to pass the id "2327" to the script archive.php, call it as: http://www.phase.org/archive.php/2327 . The value '/2327' will be passed to the script in the variable $_SERVER['PATH_INFO'], from which you can easily extract the numeric part.

Of course this url still contains the string ".php", and so isn't as elegant as we might like it to be. We want to have a script that has no extension, but still gets recognised and executed as PHP by Apache.

Fortunately Apache lets you do this quite easily with Apache's ForceType directive, which can be used either in the main Apache configuration file, or in a .htaccess file in the directory of the script. The format is as follows:

<Files filename>
ForceType application/x-httpd-php
</Files>


substituting filename as appropriate.

This allows us to be a bit more flexible with filenames, so on this site I've used the name 'byjid' (by-journal-id) for the script, so that the URL for the first post on this site is: http://www.phase.org/byjid/310

Full details on the ForceType directive can be found at http://httpd.apache.org/docs/mod/mod_mime.html#forcetype , and details on <Files> at http://httpd.apache.org/docs/mod/core.html#files .
Posted by parsingphase, 2004-04-09 19:24

Anonymous user

Login

Blog

Contact

I'm currently available for contract work in London as a Senior PHP Developer. Contact me for a CV, rates, or a chat.

Twitter @parsingphase
Email richard@phase.org
Github parsingphase
LinkedIn Richard George
Flickr parsingphase