Ampersands in Query Strings, Begone!
Why hello there! Yes, I was off the blog radar for a little over a year. We'll chat about it sometime. (I've been microblogging on Twitter though. 's fun.)
Speaking of Twitter, John Gruber just pointed out "W3C's long-ignored suggestion to use ';' instead of '&' as [an] argument separator in URLs (because '&' causes so many problems when not escaped)."
It brought back memories of my first year at AT&T Bell Labs in 1995, when I tried to promote usage of semicolons. It never took hold. Third party design agencies pretty much laughed that recommendation right off the stage.
(Y'know ... I think the last time I was laughed at that much was when I gave a fifth grade presentation on the metric system. Grade schoolers can be so very cruel, I know.)
Anyway, John, this one's for you and all those with the audacity to hope for semicolons in their query strings.
# Replace ampersands in QS with semicolons
# Add as first two rules in .htaccess or httpd.conf
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([^\&]+)\&(.+)$
RewriteRule ^(.+)$ $1?%1;%2 [E=amp:y,N]
RewriteCond %{ENV:amp} y
RewriteRule ^(.+)$ /$1 [R=permanent,NE]
For the uninitiated, here's how it works. First, the RewriteEngine must be On, so that's a given. Then we have two Rewrite Condition/Rule pairs.
The first pair looks for an ampersand in the query string. If found, we rewrite using a semicolon. (%1 and %2 refer back to the query string portions found before/after the ampersand.) We also set a variable "amp" to mark that we've encountered an ampersand, and then we repeat rule processing from the start (N).
For the second pair, what we're aiming for is a single permanent redirect (since we're swapping out one ampersand at a time). That's where the "amp" variable comes in. If it's set, we must have cleared the first Cond/Rule pair, so we now permanently redirect. No encoding (NE), please. That's it!
Consider it a first pass. I realize there are some boundary conditions I'm not taking care of yet, like leading and trailing ampersands in the QS, not to mention catching & amp; outright. (Mmm-hmm, ever try cutting-and-pasting a malformed link?)
Also, yes I know this blog presently uses ampersands ... and what about the server side? Will it treat those semicolons properly? (Can yours truly be redeemed, or has he painted himself into a corner? Stay tuned!)
For now, the above seems to work quite nicely on my Apache 2.x installation. Feedback most welcome. Don't be shy now.
Lest I forget, my sincere thanks to @gruber for singlehandedly waking me up from my over one year-long blogger hibernation. Let's see if I stay awake this time.
Update: I just noticed my last blog post also concerned query strings! Wow, what are the odds?