Lessons Learned: Dynamic Urls to Static with Mod Rewrite

Posted Wednesday, May 11th, 2005 at 02:00 AM

It’s a good thing that I have Caleb’s photo on my desktop and Chesney’s photo sitting on top of my montior because they reminded me that I have something to live for. After hours of staring at Perl regular expressions, I came very close to putting my head through my monitor.

If this makes perfect sense to you…

RewriteRule ^/?([^/.]*)/?([^/.]*)/?$ index.php?var1=$1&var2=$2 [L]

…then you are a geek. This is a rewrite rule using dreaded mumbo-jumbo called Perl regular expressions.

This all started when I started looking at timches.com’s stats and finding that I wasn’t getting as many hits after the redesign. I found the culprit to be the new dynamic pages. Apparently, search engine robots don’t like GET variables found in urls like:

http://www.timches.com/pictures/show_album.php?id=196

I used rewrite rules in .htaccess files to create an alternative “static” link:

http://www.timches.com/pictures/2005/05/196/happy_1st_birth.html

Following the example of popular CMS packages like WordPress and Movable Type, I included the title within the last part of the url, which helps get a higher listing on search engines, so I’m told.

The rewrite rule to make that static link happen is just one line:

RewriteRule ^([0-9]*)/([0-9]*)/([0-9]*)/([a-zA-Z0-9_]*).html show_album.php?id=$3&y=$1 [L]

Here’s several sites that I found helpful in learning how to do this (and for my future reference as well).

So now I sit and watch to see if indeed this helps my search engine rankings. If it doesn’t, well, that’s why I have Caleb and Chesney’s pictures close to my monitor.

Comment via Facebook