← Kirby Week 2008: Syndication Sensation

Kirby Week 2008: Pretty URLs

On the fifth day of Kirby Week, Four Island gave to me: An way of making URLs look prettier.

As I promised yesterday, today I'll be showing you how to use prettier URLs to reference your blog posts. We'll be removing the query string.

In this tutorial, I'm assuming you're using the Apache web server with mod_rewrite enabled. Otherwise, you'll probably need to read more elsewhere when we get up to the .htaccess file.

Speaking of the .htaccess file, let's do that now. In your kirbyweek08 folder, create a file called .htaccess and put this in it:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/ read.php?slug=$1 [L]

Well, that's yummy-yay-yay, but the read.php file does not yet accept a slug parameter. Let's add that functionality now with an upgrade to the read.php file. Find the line that says:

$getpost = "SELECT * FROM posts WHERE id = " . $_GET['id'];

Replace it with the following:

if (isset($_GET['id']))
 $getpost = "SELECT * FROM posts WHERE id = ";
 $getpost .= $_GET['id'];
} else if (isset($_GET['slug']))
 $getpost = "SELECT * FROM posts WHERE slug = \"";
 $getpost .= $_GET['slug'] . "\"";

This will allow read.php to parse slugged URLs. Now that Prettiful URL support has been added, you think you're done, right? Wrong. We have to fix the links to read.php in functions.php and rss.php.

Within functions.php, there are two occurances of this partial line of code:

<A HREF="read.php?id=<?php echo($sqlOutput['id']); ?>">

Replace both with:

<A HREF="http://example.org/kirbyweek08/<?php echo($sqlOutput['slug']); ?>/">

Of couse, remember to replace http://example.org/kirbyweek08/ with the path to your blog.

In addition, the following partial line of code appears in rss.php:

/read.php?id=<?php echo($getposts3[$i]['id']); ?></link>

Replace it with:

/<?php echo($getposts3[$i]['slug']); ?>/</link>

And there! You're done! You've added prettiful URL support to your blog! YAY!

Tomorrow we'll be discussing other small improvements you can make to your homebrew blogging system.

Hatkirby on December 18th, 2008 at 12:30:25pm
👍 3 👎


Replying to comment by :
Feel free to post a comment! You may use Markdown.