Note: I wrote this when I was using my own CMS for blogging. I have now moved to the darkside of Wordpress and loving every second of it.
As I was writing this site, trying to create my own blogging platform, I noticed that my perma-links were not nearly as pretty as any of the big-name blog engines like WordPress and Blogger. My posts were looking like:
http://www.kylemeyer.com/index.php?post=34
It just looks awful, and search engines (besides Google) can’t index them. But, I couldn’t find anywhere on the web that had a concise tutorial for how to set up the much more attractive and search engine-friendly links that look like:
http://www.kylemeyer.com/post/my-blog-is-so-pretty-and-awesome
Here’s how it’s done:
Note: mod_rewrite must be installed on Apache for this to work. Verify with your host if you don’t know if it is.
Step One
Index your post titles. In most gui database managers like PhpMySql, its as easy as checking a box for the appropriate column. This is to speed up the queries so the database isn’t doing full text searches for every post. The following is to do it via SQL command at the terminal.
ALTER TABLE `table` ADD INDEX ( `column_name` )
Step Two
Rewrite your links. I decided I wanted to preface my titles with “post/” so as to denote an individual post, and add hyphens where spaces are in the titles. This is the most common way I’ve seen used, although you could get creative and do whatever you’d like. I simply parsed the links differently out of the database, and used str_replace() to swap the spaces out.
For instance, instead of doing the normal parsing of
<h1> <a href="index.php?id=<?php echo $id ?>"> TITLE </a> </h1>
Generate whatever you’d like to use instead. We’ll accomodate your choice later on in steps three and four. In this case, I chose
<h1>
<a href="post/<?php str_replace(" ", "-", $title) . "\">");?>">
TITLE
</a>
</h1>
In the example above, we replaced the spaces in the title with hyphens, and are sending that instead of the id to our code to give us the correct post.
Step Three
Accomodate using Post titles as index. This is where the Php (or whatever language you’re using) comes in. Instead of pulling posts out by id, you’ll be using your post titles as the index. Here’s an example class in Php to show how we might go about this.
class PostByName {
var $post_id;
var $post_title;
var $post_body;
var $post_date;
function PostByName($title){
$header = str_replace("-", " ", $header);
$header = mysql_real_escape_string($header);
$this->setItem($header);
}
function setItem($title) {
$query="SELECT * FROM `posts` WHERE `post_header`='$header'";
$result=mysql_query($query) or die("Oops. SQL ERROR LOL");
$result_obj=mysql_fetch_object($result);
$this->post_title = $result_obj->post_title;
$this->post_body = $result_obj->post_body;
$this->post_author = $result_obj->post_author;
$this->post_email = $result_obj->post_email;
$this->post_date = $result_obj->post_date;
$this->categoryId = $result_obj->category_id;
}
}
In the example above, a class is created with all the variables of our blog posts. In the constructor, we remove the hyphens we used in the URL in step two, to get back to the original, indexed title. We then escape it and set up and use a very simple SELECT FROM WHERE query using the title to assign our object its attributes. We now have an object we can instantiate and read the attributes of, simply by passing in a hyphenated blog title.
Note: During development of this, I had trouble using certain characters such as ampersands in my post titles. Be wary.
Step Four
Modify or create your .htaccess file. If the file isn’t already in the root directory of your web directory, you’ll want to create it. To get my links to look like…
http://www.kylemeyer.com/post/my-blog-is-so-pretty-and-awesome
…I used the following .htaccess addition:
Options +FollowSymLinks RewriteEngine On RewriteRule ^post/([a-zA-Z-]*) /index.php?post=$1 [QSA,L]
Explanation
Options +FollowSymLinks turns on, guess what, symlinks in the directory and allows the rewrite engine to work its magic. Which, of course, the next line turns on! The RewriteRule is a bit more complicated. It uses regular expressions for wildcards. ([a-zA-Z-]*) is the wildcard I’m using. It allows lowercase and uppercase a-z along with hyphens. $1 simply denotes the first wildcard used. Finally, [QSA,L] tells the server that this will be the last rewrite rule used, and allows variables to be appended (like ?get=lol). You could rewrite your URLs into a number of different ways, such as
RewriteRule ^post/([0-9]+)/([a-z]+)/([a-zA-Z-]*) /index.php?post=$3 [QSA,L]
Which allows you to insert the year, followed by the month, and finally the post title. It would end up looking like the following:
http://kylemeyer.com/post/2007/may/my-post-is-so-pretty-and-awesome


