NYCPHP Meetup

NYPHP.org

[nycphp-talk] Many pages: one script

Michael Sims jellicle at gmail.com
Sun Aug 5 14:24:07 EDT 2007


On August 5, 2007, Elliotte Harold wrote:

> I'm considering a simple site that I may design in PHP. PHP is probably
> the simplest solution except for one thing: it carries a very strong
> coupling between pages and scripts. As far as I've ever been able to
> tell PHP really, really, really wants there to be a single primary .php
> file for each URL that does not contain a query string (though that file
> may of course invoke others).
>
> For the system I'm designing that simply won't work. In Java servlet
> environments it's relatively trivial to map one servlet to an entire
> directory structure, so that it handles all requests for all pages
> within that hierarchy.

You have to think of the whole file-serving hierarchy here.  Apache gets a 
request for an URL.  What will Apache do with it?  Apache will find a file 
somewhere that matches that URL, and then Apache will either send that file 
off for further processing (if the file is registered as such within 
Apache) or Apache will just send that file off to the browser as-is.  PHP 
doesn't even enter into the picture until the decision you're talking about 
has already been made by Apache.

I don't know much about java servlets, but I strongly suspect it's the 
same - the Sun web server or whatever you're using is making that decision.  
It may appear to be "simpler" than the PHP/Apache combination but it really 
isn't.  Perhaps it is better integrated because both the web server and the 
programming language are products of one company, but it's not any simpler 
when it executes.

In any case the correct answer is just to tell Apache to serve file X for 
every URL that looks like Y or Z or W.  Mod_rewrite.  That's what it's 
there for, and it does its job well, and it can be as simple as a couple 
lines in an .htaccess file.

No doubt one could partly handle this with PHP files:

foo.com/index.php - lots of code
foo.com/dir1/index.php - PHP file sends everything to foo.com/index.php
foo.com/dir2/index.php - PHP file sends everything to foo.com/index.php
foo.com/dir1/dir3/index.php - PHP file sends everything to foo.com/index.php

but telling Apache to use foo.com/index.php for all requests is simpler and 
less error-prone.  It can be quite simple:

Contents of foo.com/yourdir/.htaccess file:
-------------------------------------------------------
RewriteEngine On
RewriteOptions inherit

RewriteBase /yourdir

RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+)/ index.php?var1=$1&var2=$2
RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+) index.php?var1=$1&var2=$2
RewriteRule ^([0-9A-Za-z]+)/ index.php?var1=$1
--------------------------------------------------------

Now, any request for foo.com/yourdir/anything/anythingatall will be sent to 
foo.com/yourdir/index.php, which will see the extra "directories" as URL 
variables.  The user will not know what's happening - they'll continue to 
see the "directories" in their browser status bar.


Michael Sims




More information about the talk mailing list