M-Solo.com

Clean URLs Finally!

December 10, 2006

It all started with trying to find an easy and safe way to template my website. With my old design, I used Smarty’s Templating Engine. Smarty was easy to install and has a bunch of very cool features, I just was not a big fan of the syntax, it made my code kinda sloppy, and thought I would learn more creating my own. Without getting into details, I used a hash table to associate page attributes with keys. I then used get variables, to fetch my page. The problem is that the pages looked like this index.php?page=about. It works, but it’s not pretty, and it’s also a pain to say “Check out the pictures on www.m-solo.com/index.php?page=photos“.

So I did some research and found two ways to accomplish clean URLs. For those who don’t Clean URL’s will take index.php?page=about and turn it into /about/.
The first was using the Apache Force Type Method. This Apache directive forces the server to consider a file as being of another type than what is expected by default (documentation on ForceType).

So I dropped the command below into a .htaccess file on the root of my directory.

ForceType application/x-httpd-php

I then exploded the “/” to determine the key representing the page. I used
$page_key = explode(”/”,$HTTP_SERVER_VARS[’PATH_INFO’])

What I got was:

www.m-solo.com/index/about/

Now this worked, but it was not exactly what I wanted. I really envisioned www.m-solo.com/about/. So after doing some research, I discovered the Push CX blog discussing the Mod Rewrite method. This method provides a rule-based rewriting engine to rewrite requested URLs on the fly. (Mod_Rewrite). I copied:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

into my .htaccess file and voilĂ  clean URLs.

Posted in Web

Comments (0)« Previous Page