We already covered the basic idea behind the friendly URL and how it can bring more visitors to your web page. If you missed the post where we discussed this and implemented the mod_rewrite module in Apache, you can read all about friendly URLs and our setup right here. One growing, important, feature nowadays is the URL. People didn’t come up with the idea of manipulating the URL people type in for no reason. It should be simple and easy to navigate through a website, right? Today we are going to make navigation via URL’s even simpler, especially for dynamically build up websites. This is the idea:

Instead of using id’s in the URL we are going to use the page title. We will not include the id of the object in the URL because if we had to do that, this wouldn’t make it easier to navigate through since you have to know both the id and title of the object. In this post we will take a look at how we can use a game title instead a game id in our URL. The visitors of the site can easily guess the title of the game and just type it in!

Then end result will look something like this:

http://www.dosspot.com/games/sam-and-max-hit-the-road/

Looks nice? It looks a lot better than the current URL:

http://www.dosspot.com/details.php?gameid=1

So, here is what we need: A PHP function which takes a string and turn it into friendly chars we can use in our URL, a new column in our database table and finally some new lines in our .htaccess file.

OK, so what do we do with our PHP function? We keep it simple and effective. Anything not matching a char between a-z and A-Z or a number, 0-9, will be removed from the title. Every space we find we replace with a -.

Example: Command and Conquer: Tiberan Dawn will do. This title would be suitable as a URL. We use our function and it will look like this: command-and-conquer-tiberan-dawn. Simple, nice and really cool!

The implementation of this is not that hard. We extract and remove all bad chars in the title. It’s very straightforward. Five lines of code:

function createPermaLink($string)
{
$string = preg_replace("/(:|;|-|\"|\/|\(|\)|\')/", "", strtolower($string));
$string = preg_replace("/(\s)/", "-", strtolower($string));
return $string;
}

We will use this function everytime we want to link to a game page. We will send the game title as the argument into the createPermaLink function and it will return a clean and URL friendly string back to us.

Next up we go to the MySQL database table we want to match our perma link with. We add a new coloumn named perma as a varchar length 100. We take the game title and use our newly created createPermaLink function and copy the text and paste it into the new database column. So, the database table might look like this:

ID Title                                                      Perma
1    Sam and Max Hit the Road             sam-and-max-hit-the-road

Now we are able to do a SQL statement and try to select the row with a Perma value matching the incoming perma value from the URL. To be able to send a perma via the URL we need to add lines to our .htaccess file. Again, very easy:

RewriteRule ^([a-z0-9\-]+)/$ /game.php?perma=$1 [L]

Here I have a rule saying: Anything that matches a-z chars or 0-9 integers or a – is valid and it’s redirected to my game.php page with a querystring attached named perma with a value. The value in our case will be the game title transformed by our createPermaLink function.

So in our game.php file we simple write a SQL statement to fetch the corrent row with our Database class:

$perma = mysql_real_escape_string($_GET['perma']);
$result = $db->fetchQuery("SELECT * FROM games WHERE perma = '$perma'");

Pretty simple huh? The first line is just making sure that no hackers can break into our database and after that we use the $db object which is an instant of our Database class and we call our fetchQuery method to retrieve the row we want.

Three different areas we need to modify but only with very little code we are able to add the page title in the URL quite simple. The URL’s is a lot nicer and SEO loves it!