Mowser domain mapping and mod_rewrite fixes
My fellow Mobitopian Jim Hughes took some time yesterday to make a mobile version of his website using Mowser. Then he took some more time to point out some bugs to me on the wiki, which I tackled last night after the munchkin went to bed and I woke up from a tryptophan-induced coma. While in there bug fixing, I was able to enhance and break few things as well.
First was a problem with the wiki and urls that started with"./". I popped in a hack so that it would strip it out, which doesn't help existing insane ././././ type urls, but will at least prevent the URL rewriting process from creating them. Next was a *very* weird error where after Jim mapped the m-dot version of his site to Mowser, m.feetup.com/blog would actually pull up *my* blog (the one you're reading) instead. This should be impossible, as the default virtual domain on my server is Mowser, which is what allows me to map arbitrary domains in the first place. After some investigating, it turns out that the way I was doing domain mappings was a bit braindead, and any URL that already existed in mowser.com wouldn't work - i.e. /feeds or /keywords, etc. I had forgotten that I had mapped /blog to redirect to my personal blog, and that's what was causing the problem.
So I delved into the problem, coming up with a few less-than-optimal solutions at first, before I finally RTFMed the Apache manual enough to discover that I could do the re-mapping logic in mod_rewrite rather than doing it in PHP. Here's what it looks like:
RewriteCond %{HTTP_HOST} !^m.mowser.com$
RewriteCond %{HTTP_HOST} ^m.(.*)$
RewriteRule (.*) http://mowser.com/web/%1%{REQUEST_URI} [R,L]
Basically, it looks for m.* domains that are mapped to the Mowser server, and if it isn't m.mowser.com, it'll automatically redirect to a mobilized version of that domain, but without the m. In other words, m.example.com will pull the page from example.com. At first I was mapping it to www, but after getting a bunch of requests, I'm simply stripping out the m.* instead - hopefully this doesn't break many sites. The magic, by the way, for those keeping score at home is the %1 in RewriteRule - that maps to the last RewriteCond match, making what was a few lines of PHP, into a simple mod_rewrite condition. Another fix I made is the addition of the REQUEST_URI, which allows publishers to "deep link" into their mobile version - this wasn't working before.
Also, rather than redirecting to the m.mowser.com version, which *always* returns the vnd.wap.xhtml content type that both IE and Firefox can't handle, I'm now redirecting to the mowser.com version, which will auto-detect mobiles and/or PC browsers and deliver the appropriate version so PC users can see an approximation of what the mobile site will look like.
While messing with all this, I took the chance to update the Code Examples page with a couple new mod_rewrite rules. If you'd rather not deal with Domain mapping, you can now just use a version of the above mod_rewrite rule instead on your own server. I also adapted that long PHP code which checked for mobile devices into a long list of RewriteCond's as well, so you can detect and redirect from your own .htaccess file. Both examples are on the wiki.
Finally, I decided to make a little button publishers can use on their weblog to promote their mobile version as well (similar to the different feed readers out there). Here it is:
I've linked it to m.russellbeattie.com as an example of how you'd use it on your blog. You can also simply map it to something like http://mowser.com/web/example.com as well.
Thanks again to Jim for using Mowser, helping find these bugs and writing them up for me - being able to tackle this stuff as it comes in really helps a ton!
-Russ