Over-thought Solutions

Sunday, December 16, 2007

After posting my Painfully Regular Expressions, Chuck commented that on his site, he threw the entire URL to the handler and let PHP parse it.

His RewriteCond probably looked something like this:

RewriteRule ^(.*)/?$ /handler.php?url=$1

And his PHP code probably looked like this:

$url=explode('/',$_GET['url']);

Alternatively (and where I ended up where I ran into problems with the way rewrite handled ampersands (&) in the tagnames, e.g., http://sewcrates.com/tags/D&D):

RewriteRule ^(.*)/?$ /handler.php

And the PHP code:

$url=explode('/',$_SERVER['REQUEST_URI']);

Both of these, by the way, beats the crap out of my PHP code for my original RewriteCond formulation:

$url=array($_GET['a'], $_GET['b'], $_GET['c'], $_GET['d'], $_GET['e']);

And has the added benefit of handling any number of parameters.

Sigh. This is one of those situations where my own cleverness got the best of me. I’ve since reconverted my .htaccess file to Chuck’s more elegant solution. I leave it to PHP to parse the URL. And with this egg on my face, I’ll return to my little programming hole.

One good thing did come of my earlier exercise. I learned the basics of regular expressions. After reading Chuck's mail, I created the new RewriteCond in only a few seconds. Before I went through my Painfully Regular Expressions, it would have taken much longer. I could have probably found Chuck's solution by Googling a bit--but I wouldn't have learned much. And look how many words I pushed to explain my failure!

 Seattle, WA | , ,