I have been looking at some sites that pretend that there is a directory structure in the URL and wondered 'how?'.
I am taking control of a website at work and have looked over the code. They have a database for all the pages and they are created dynamically.
I can get the homepage working on my local server but I don't have a clue as to where to start with the fake directory structure. An example is http://www.bankcharges.com/bank-charges-advice/
- there is no directory for this but the content is in the database.
How have they done this?
The code this I think is related to is:
index.php
:
<?php
include('includes/functions.php');
$activeTab = "navhome";
$sent = false;
$title = (isset($_GET['title']))? mysql_real_escape_string($_GET['title']) : 'Home';
$title = str_replace('-',' ', $title);
if($title != '') {
$sql = "SELECT *
FROM contents
WHERE name LIKE '%$title%'
LIMIT 1";
$result = @mysql_query($sql);
$row = mysql_fetch_assoc($result);
}
//Set page title
$pagetitle = (isset($row['name']) && $title != 'Home')? ucwords($row['name']) : "Bank Charges";
?>
functions.php
:
<?php
include('database.php');
include('settings.php');
//Nice URL's
function url($str){
$arr = array('!','"','£','$','%','^','&','*','(',')','_','+','{','}',':','@','~','<','>','?','|',',','.','\\','/',';',']','[','\'');
$str = str_replace($arr,"", str_replace(" ","-",strtolower($str)));
return $str;
}
function isEven($v){
if($v % 2 == 0) return true;
}
?>
mod_rewrite is the apache module that allows this to occur. Other web servers have their own implementations.
For a beginners guide, check out this blog post.