Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Pointing all the web pages in a directory at a database

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=$1


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 .....
(written 2008-08-30 09:23:26)

 
Associated topics are indexed under
A603 - Web Application Deployment - Further httpd Configuration
H305 - PHP - Web server configuration

Back to
The Rise and Rise of First Bus Fares
Previous and next
or
Horse's mouth home
Forward to
Injection Attacks - avoiding them in your PHP
Some other Articles
Calling procs in Tcl and how it compares to Perl
Reception
Server overloading - turns out to be feof in PHP
Injection Attacks - avoiding them in your PHP
Pointing all the web pages in a directory at a database
The Rise and Rise of First Bus Fares
Does fruit and veg drag on?
Easterholic
What is my real and my effective ID? [Linux]
The Longest Possible Day
1892 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho