Main Content

Pointing all the web pages in a directory at a database

Archive - Originally posted on "The Horse's Mouth" - 2008-08-30 09:23:26 - Graham Ellis

Do you want a web directory of pages, where each of them has its own page name (URL), but there isn't really a separate page for you to maintain - all the data is held in some sort of database on your server, and the pages are created automatically from the data as they're requested?

You can do this very easily, using the mod_rewrite Apache module on your server, and a short PHP script. Here's a "shows you how" example.

1. Create the directory in which you want to pretend the pages live.

2. In that directory, create a file called .htaccess which contains mod_rewrite instructions to pass ALL appropriate URLs to a single PHP script.

3. Create a PHP script which generates the pages from the database or other information source.

I've placed an example at http://www.wellho.net/smr/index.html (of course, there isn't REALLY a file called index.html there!). And that links on to pages like http://www.wellho.net/smr/HLD.html (which, of course, isn't really there either!).

So what do the few (two!) files that I've told you about look like.

The .htaccess file is as follows:


AddType application/x-httpd-php .html
RewriteEngine On
RewriteRule ^index\.html$ stations.php?stn=ZZZ
RewriteRule ^$ stations.php?stn=ZZZ
RewriteRule ^(...)\.html$ stations.php?stn=


And the "magic" PHP script reads like this:


<?php
# Set up access to the data, and get content
# mod_rewrite has supplied the page name as
# the stn element of $_REQUEST
require ("tools/datasource.inc");
$wantpage = $_REQUEST[stn];
$fill = getcontent($wantpage);
# If there's a problem with the page requested,
# send a "page not found"
if (! $fill) {
   header("HTTP/1.0 404 Not Found");
   exit();
   }
/* ----------------------------------------------- */
?>
<html>
<head>
<title><?= $fill[title] ?></title>
</head>
<body bgcolor=#FFFFCC>
<?= $fill[pagedata] ?>
<hr />
<h4>Railway Stations of Great Britain</h4>
This is a demonstration of how the Apache mod_rewrite
module can be used to serve a whole directory of web
pages through a single PHP script, with the data being
read from a database. You can learn about the techniques
used on our
<a href=http://www.wellho.net/course/ptfull.html>PHP
techniques Workshop</a><br /><br />
Copyright, Well House Consultants, <?= date("Y") ?><br />
Contact: <a href=mailto:info@wellho.net>info@wellho.net</a>
or +44 (0) 1225 708225.
</body>
</html>


There's one extra function call in here that I've not supplied as part of this example - and that's getcontent which I have referred to in an include file. You'll need to write your own code for this, depending on what your data source is and how you want to format the pages. I'm very happy to share techniques and data with you on our PHP techniques Workshop and / or PHP Programming Course, but as it's going to be a very different piece of code under the hood of each application, it's not reproduced here.

And a reminder ... in order to be able to run something like this on the web server you use, you do need to have PHP and mod_rewrite installed and accessible to you. I know that sounds obvious when I point it out, but you would be surprised .....