.
.
.

Rants and Articles.

Revenge of the URI

It is April. Month of the fools, and the… um… Aprils, I guess (sucks to be you). Outside, dogs are barking, kids are playing and people are going on with their lives, oblivious to the goings on of the latest —and, by this time, obligatory— Overcaffeinated’s useless and utterly invisible redesign.

The poor, poor fools.

As usual, this redesign (or major tweaking) dramatically improves several key aspects of the site’s usability. Aspects that no one had ever remarked upon or —most likely— even cared about. But, also as usual, it gives me a warm, fuzzy feeling inside, and that’s ultimately the reason why I do most of the stuff I do. So there.

If you head over to the different sections of the site, you may notice that the URI’s have changed and no longer point to a specific HTML file. Instead of http://overcaffeinated.net/about.html, we now have http://overcaffeinated.net/about/. The reasoning behind this change is that now I can change the about.html file to be generated by a different CMS (which may use another file extension), to be coded in PHP (about.php) or to be hacked together out of pocket lint, for all the URI cares. Its location will stay the same, since I only need to change a rule in my .htaccess file for it to point to the new file.

The underlying structure of this site may change tremendously, and all bookmarks would still work (well, the ones made after this change anyway). This also has the added advantage of simplifying URI’s, and allowing people to remember parts of the site easily. The methodology is explained in two ALA articles: Slash Forward and How to succeed with URL’s, so I won’t go into the nitty gritty of how .htaccess and mod_rewrite work. Suffice it to say that a .htaccess file can be placed in any directory of your site and Apache will use it to determine access permissions to the files in that directory. Mod_rewrite is an Apache module that allows you to create dynamic URL rewrites with regular expressions, so the url you convert from/to can be mind bogglingly complex or really simple, depending on which end of it you’re standing.

The rules I created for this site are very simple. For the static pages (about, abridged…) I use this rule:

RewriteRule ^about(/)?$ /about.html

Mod_rewrite parses that as follows: The RewriteRule part indicates we’re specifying a Rewrite Rule (told you this was easy!). The next part is what the user will see in the address bar of his browser. ^ means the start of the URL (the root of the site), then we have the word (about) and then the (/)?$ bit, which decomposes to this: (/) is a piece of delimited text (the text is “/”), the ? tells mod_rewrite that we’re to expect zero or one of the delimited text (hence, you can go to http://overcaffeinated.net/about/ and http://overcaffeinated.net/about and they both point to the same place). The $ marks the ending of our regular expression. The next part is the actual location of the file we’re pointing at (/about.html).

The next example is more complex, and you can see it in action if you head over to the comic section. The rule is this:

RewriteRule ^comic/date/(.+) /cgi-bin/comic.cgi?date=$1

What this does is change the usual comic url from the aesthetically challenged http://overcaffeinated.net/cgi-bin/comic.cgi?date=yyyymmdd format to the strikingly beautiful http://overcaffeinated.net/comic/date/yyyymmdd form (makes you want to shed a tear, doesn’t it?). Notice the (.+) bit at the end of the first section of the rule. That’s where we take the part of the URL that changes (the yyyymmdd part) and reference it later with the $1 thingie.

These changes have been implemented all over the site, along with a change to simplify internally referencing links, which I had foolishly set up with a fully qualified path. For instance, I was using <a href="http://overcaffeinated.net">... as a link to home, instead of <a href="/">..., which ultimately amounts to the same and looks much prettier. I was doing this because sometimes I’m just plain dumb. But now it’s been corrected, and the code is looking cleaner everyday.

Doesn’t that give you a warm, fuzzy feeling?

sergio on April 06, 2004  permalink

Comments

06 Apr 14:45
sergio spake thus:

An added benefit of this, that I just noticed, is that pages that are dynamically generated from cgi’s are no longer lumped in the same slot in my site statistics, so this gives me greater accuracy in determining which entries/comics are the most seen. Cool.

And of course, Google loves this kind of links (the bastards!)

06 Apr 23:26
Kether spake thus:

Well… I just posted this so that you might wonder who might have read your article, fully understand it… and post a comment to share some experiences with you…

Sorry… this is not me… I just wanted to say hi… and to see if you wanna go tomorrow to the Review for a beer… 8pm… with the galos, as usual….

cya

06 Apr 23:37
Kitta spake thus:

I’m meant to be cleaning, but now you got me looking into this. Damn you Sergio! ;)

07 Apr 10:59
sergio spake thus:

Well, Kitta, as the little Red Monkey once said: “A creative mess is better than an idle tidiness”. Or something to that effect. Damn if I can get the little creep to say it again…

Say it! *reload* Say it! *reload* AAARRGH!!!

09 Apr 13:26
rio spake thus:

nice blog! just wanted to drop a notice that the International Webloggers’ Day is June 9, 2004! http://www.intlblogday.tk

happy april

Latest Comic

News from the 'net

⇒ XHTML | CSS | 508
.
.