URL redirections

After a refreshing vacation mainly away from computers, I’ve finally had the time to work on some of the issues that have been bothering me on our site. My main problem has been that when people look at several strips of Jermut or pictures from the Life of Jalo, the statistics we get in Urchin are problematic. The following will outline how the Life of Jalo has been fixed and offers some hints on how to do it yourself. The discussion will be technical and source code is shown. You have been warned.

In detail, the problem is that Urchin doesn’t make a difference between the URLs /loj/ and /loj/?pic=20040928 unless you dig deeper into the reports. And, to tell you the truth, URLs like the one used in Jalo’s photoblog are ugly to me. There has to be a better approach to it all. Since I’m a programmer (among other things), it was time to start looking for a better solution. The end result, my goal, was to have the images in /loj/images/ accessible via the URL /loj/year/month/date/ without having to do anything else than uploading the days image to the server. The best (and most available solution) to achieve my goal was to use Apache’s mod_rewrite to do all of the dirty work.

Mod_rewrite is a powerful tool. But with that power comes quite a bit of complexity too. The first problem is that you have to be familiar with regular expressions to work with it. My background with regexs is flaky, so I picked up my copy of Friedl’s excellent book on the topic and got to work. The end result is the following .htaccess snippet:

RewriteEngine On
RewriteBase /loj/
RewriteCond %{REQUEST_URI} /loj/+
RewriteRule ^([0-9]{4,4})/([0-9]{2,2})/([0-9]{2,2})/$  ?pic=$1$2$3 [NC]

Quite simply, what this does is instead of opening the URL /loj/YYYY/MM/DD/ it opens the URL /loj/?pic=YYYYMMDD. This still leaves the old style URLs unharmed and allows external linking to the images from other sites. After this, it’s just time to write some PHP code to find the correct file and serve it. A lengthier discussion on how the individual pages are generated will follow sometime soon. I just have to work out a few quirks still (bugs or features, depending on you point of view).

Now on to fixing this for Jermut, fixing the metadata embedded in Jalo’s pictures and other improvements. Stay tuned…

Leave a Reply