onOn 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
.htaccessfile.Speaking of the
.htaccessfile, let's do that now. In yourkirbyweek08folder, create a file called.htaccessand 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.phpfile does not yet accept aslugparameter. Let's add that functionality now with an upgrade to theread.phpfile. 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.phpto parse slugged URLs. Now that Prettiful URL support has been added, you think you're done, right? Wrong. We have to fix the links toread.phpinfunctions.phpandrss.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.
DEC
18

Comments