Tech Note: LGF URL Makeover with mod_rewrite
This weekend our aging, creaky, rather unfriendly URL scheme got an extreme makeover, courtesy of the Apache htaccess file and mod_rewrite.
The cool thing is that this redesign doesn’t break any old URLs; if you’ve bookmarked an LGF page, or if you follow an older link from another site, it will continue to work as always.
The old scheme used HTTP query strings, in that ugly sort of techie way you’ve seen many times; for example:
This would show an LGF front page entry on its own page, without the reader comments. After the revamp, this URL becomes:
Isn’t that prettier? And it has the advantage of being easier for search engines to index, too.
To show an LGF page with reader comments, the URL would be:
To show a particular comment:
Internally on the server, all of these new types of URLs are converted to their old style addresses with those ugly query strings, so none of the LGF code needed to change. Which is always good.
For the geeks in the crowd, here are the mod_rewrite rules that accomplish this feat of prestidigitation:
RewriteRule article/(.*)/comments/(.*) /article/$1$2
RewriteRule article/(.*)/comments/ /weblog/?entry=$1
RewriteRule article/(.*) /weblog/?entry=$1""http://lgf.com/weblog/logo.gif" data-fancybox-group="dfd54edd" title="logo.gif" class="image">Image: logo.gif”) can be found when the above URL redirection scheme is used. Without the BASE tag, the browser requests those files from inside a nonexistent folder named /article/.
But Opera (including the latest version as of this writing) doesn’t properly pass the base URL to Javascript Ajax calls. Apparently they need a little bit better communication between their HTML and Javascript engines.
The fix was annoying but relatively easy: just change the Ajax calls to use absolute path references to their scripts.