More on directory-style paths

The following was emailed as a response to a user query about the techniques mentioned at
http://phase.org/blog/2640_blog_archive_part_two_a_better_url



The first thing to know is that any PHP script can be called with a URL that continues after the script filename,
eg: (these URLs don't work, they're just examples)
http://www.phase.org/journal.php can also be called with the URLs
http://www.phase.org/journal.php/journalname/stuff/things/etc
http://www.phase.org/journal.php/?id=wechsler

As far as the webserver is concerned, these are all simply requests for journal.php with 'some stuff' added on the end.

When you pass arguments to a PHP script with a query string (?key=value&key2=value2 etc), you can (as I'm sure you know) read these as $_REQUEST['key1'] etc. (You could also read them as $key1 etc, but this method is dangerous).

When you extend the URL with slashes, you can read the extra parts as $_SERVER['PATH_INFO'] (at least on Apache, I've not tested on IIS). This will give you one long string, ie:
$_SERVER['PATH_INFO']='journalname/stuff/things/etc';
You can then split this apart with
$pathparts=explode('/',$_SERVER['PATH_INFO']);
and then read $pathparts as an array - use print_r to examine it.

This now lets you avoid query strings, but you've still got a URL with ".php" in it. To get rid of this, you need to rename the script (eg to just 'journal'), but you also need to tell the webserver to treat that script as a PHP file (because it won't usually know, if the extension is missing). How to do this can vary, and you might not be able to do it if you don't have full control over your webserver.

I use the Apache directive "DefaultType application/x-httpd-php" in a .htaccess file in the directory that contains the renamed scripts.

However, even if you can't do this, you can still use directory-style access, just keep .php in the script name.
Posted by parsingphase, 2005-12-04 14:16

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