Recently I was writing a database driven website, where all the content was within a database and the only real page was index.php.

This is easy enough to do, but of course, you need to tell index.php which page you want so you end up with URL like http://www.deaconsworld.org.uk/index.php?PageID=12 or  http://www.deaconsworld.org.uk/index.php?PageID=36. Not the most user or SEO friendly. What I really wanted was URL that looked like this:  http://www.deaconsworld.org.uk/contact/ or http://www.deaconsworld.org.uk/about/

It turns out that it was actually quite simple. The principle behind it is to use a Apache re-write to redirect everything that isn’t a ‘real’ page to index.php but leave the URI available for php to process.

Let’s say you have a database called tblPages that looks like this:

+-----------+-------------------------+------+-----+---------+----------------+
| Field     | Type                    | Null | Key | Default | Extra          |
+-----------+-------------------------+------+-----+---------+----------------+
| PageID    | int(10) unsigned        | NO   | PRI | NULL    | auto_increment |
| Title     | varchar(255)            | NO   |     | NULL    |                |
| Content   | text                    | NO   |     | NULL    |                |
+-----------+-------------------------+------+-----+---------+----------------+

Hopefully the table is self explanatory, The PageID is a unique number, Title is going to be the URL and Content is the HTML of the page.

Next you need a rewrite statement, this can either be put in the virtualhost declaration or in an .htaccess file. Again this would look like this:

# Switch the rewrite Engine on
RewriteEngine On
# Set the base correctly
RewriteBase /
# Ignore and don't process any 'real' files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# finally, rewrite everything else as index.php and stop processing
RewriteRule . /index.php [L]

Next comes the magical bit. Although we can now type our URL in as http://www.deaconsworld.org.uk/contact/, Apache will send us to http://www.deaconsworld.org.uk/index.php. However, the $_SERVER[REQUEST_URI] will still contain http://www.deaconsworld.org.uk/contact/

Next we need to do a bit of work in PHP:

PageID=GetPageID($_SERVER[REQUEST_URI]);

$SQL_GetPageInfo=mysql_query
	("SELECT Title,Content FROM tblPages WHERE PageID=$PageID");
$Details=mysql_fetch_array($SQL_GetPageInfo);

print $Details[Content];

function GetPageID($URI) {
	$PageTitle_array=explode('?', $URI);
	$PageTitle=$PageTitle_array[0];
	$PageTitle=rawurldecode($PageTitle);
	$PageTitle=trim($PageTitle, '/');
	$SQL_GetPageID=mysql_query
		("SELECT PageID from tblPages WHERE Title='$PageTitle'");
	return mysql_result($SQL_GetPageID,0);
}

Again, the code should be pretty self explanatory.

So there you have it. The only things missing are a default page for when some enters http://www.deaconsworld.org.uk and to tidy up the security, but I’ll let you work that one out.