Friendly URLs

If you have a CMS that has unfriendly URLs such as “/?id=6785″, or similar, you could use this little tip.

Add a .htaccess to trap 404s (missing pages). chmod it to 644. In it, place this: “ErrorDocument 404 /404.php”

Now, for the interesting part. The following script assumes your table is called ‘pages’, your database connection script is held in ‘.htDB.php’, and stuff. Read it to figure out more.

<?
$contact='kverens@contactjuggling.org';
include('.htDB.php');
$r=preg_replace('/^\//','',$_SERVER['REQUEST_URI']);
$r=preg_replace('/\?.*/','',$r);
$r=addslashes(urldecode($r));
$q=mysql_query('select id from pages where name="'.$r.'"');
if(!mysql_num_rows($q)||ereg('/',$r)){
 mail($contact,'[yoursitename.test] missing page',
  $_SERVER['HTTP_HOST'].' '.$_SERVER['REQUEST_URI']."\nfrom: ".$_SERVER['HTTP_REFERER']);
 header('Location: /');
 exit;
}
header('Status: 200 OK');
$r=mysql_fetch_array($q);
$id=$r['id'];
include('index.php');
?>

Note that this grabs the page’s ‘id’ from teh table. If you use a different scheme, then obviously, you’ll need to amend this.

6 Responses to “Friendly URLs”

  1. online poker says:

    167 I like it like that. ewqrb2

  2. online poker says:

    5659 I like it like that. ewqrb2

Leave a Reply