In this tutorial we'll be using Apache's Files
and ForceType directives to keep the users address bar nice and tidy.
We'll be using PHP to serve up the dynamic content with the help of
Pear's Numbers_Roman class to display numbers as roman numerals.
Friendly URL's
Every good website should not only be functional, but also look
good. Why would that exclude the address bar of the user's browser? It
doesn't. Even with dynamic content
you can have clean and even uber cool urls for all of your pages. No
one wants to see your question marks and ampersands. Even numbers are
boring.
You've probably seen this in place already, but of course you never
knew how those developers achieved such a task. Which is why you are
here. An example of a friendly url would be
http://yoursite.com/articles.php/2901/. Compare that to
http://yoursite.com/articles.php?articleid=2901. The horror!
The benefits are not only clean looking urls but also urls which are easy to remember an to pass on to hot chics and family.
In this tutorial we'll be using Apache's Files / FilesMatch and
ForceType directives to keep the users address bar nice and tidy. We'll
be using PHP to serve up thedynamic content with the help of Pear's Numbers_Roman class to display numbers as roman numerals.
Apache ForceType
First we will have a look at how
requests will be handled by Apache. Apache has two directives, which
will allow us to send a particular file to the PHP engine for parsing.
I use this because I like to leave the .php out of the url (as was in
the example we looked at). So if we had a file named "article" with valid PHP code, then we could send this request (http://yoursite.com/article) to the PHP engine for parsing. More so, this request (http://yoursite.com/article/other/info) would also be sent to the PHP engine.
Let's look at the FilesMatch and the Files directives. They are
essentially the same, except that FilesMatch allows you to use a
regular expression to match the names of files. For the purpose of this
tutorial, we will just be using the Files directive.
<Files article>
ForceType application/x-httpd-php
</Files>
?>
This will make sure that all requests to http://yoursite.com/article will send the file "article" to the PHP engine for parsing.
Back the truck up! ForceType? What's that? That is the second
directive we are going to use to tell Apache to treat this request as
if were for a PHP file.
Let's try it! Make a file named .htaccess and put this in it.
<Files article>
ForceType application/x-httpd-php
</Files>
?>
Now make a file named article (yes, without an extension) and put this in it.
<?php
echo 'This thing works!!<br />';
echo 'Request_Uri is: '.$_SERVER['REQUEST_URI'].'<br /><br />';
echo 'This could come in handy!';
?>
?>
A pinch of PHP
Ok. We saw what we need to do in
order to have Apache behave properly. Now we can unleash the PHP beast
in each of us to generate useful content. We will go ahead and stick to
our articles example. Let's say you have an site database with an articles table. Here's the table structure:
CREATE TABLE
`articles` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`title` varchar(75) NOT NULL default '',
`article` text NOT NULL,
PRIMARY KEY (`id`)
);
?>
Ok. Just make sure you don't have more than 256 articles.
Now, let's decide on how we want the urls to look. Actually, let me
decide. http://yoursite.com/article/xii looks pretty damn cool. Now we
just need to write the parse the REQUEST_URI, query our database, and
then display the article. Sounds easy enough, right? Let's get to work.
<?php
$article_roman = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1, strlen($_SERVER['REQUEST_URI'));
$article_id = Numbers_Roman::toNumber($article_roman);
$result = mysql_query("SELECT * FROM articles WHERE id = {$article_id}") or die(mysql_error());
$article_data = mysql_fetch_assoc($result);
echo "Title: {$article_data['title']}<br /><br />";
echo $article_data['article'];
?>
?>
Let's go over this code line by line. The first two lines just parse
out the REQUEST_URI and get the last part of it (the roman numeral).
Then it converts that to a numeric value using Pear's Numbers_Roman
class.
The next two lines query the database for the article with the appropriate id.
The last two lines echo the title and content of the article.
So that's really all there is to it. You can take it much farther and
code an entire site like using this method and not just part of a site
(such as articles). There are lots of different methods you can use to
pass and parse data out of the REQUEST_URI.
|