Coding

Selective Autoredirection

Tuesday, 13th July 2010, sheilaellen (http://blogs.bluegumtree.co.uk/vista/)

Imagine you have two versions of the same site and one of them is optimised for mobile devices.  Aside from the root folder, the URL pattern is identical for both versions so to map between the versions you simply add or remove “/mobile”:

  • http://www.mysite.com
  • http://www.mysite.com/mobile

Most pages on each version have a matching partner on the other but, importantly, this isn’t the case for all pages.

Although you have a version optimised for mobile devices, you can’t be sure that they’ll always land on it when they visit your site so you want to:

  • detect mobile devices when they land on your site
  • automatically redirect them to the mobile version if they’ve landed on the default version but only if a mobile version of that page actually exists.

Ideally, in order to avoid swapping a perfectly good page for a 404 error you want to make the check before initiating the redirection.  You don’t have access to the Apache config files but you can use .htaccess files.

I’ve been puzzling over this conundrum for a while, trying to find a solution that has as little impact on the rest of the system, aside from doing what it’s very specifically required to do, and doesn’t open up a cross-site scripting vulnerability.  Last night, I think I cracked it and as I found very little on this myself when scouring the web, I’ll share it here.  Hopefully if you spot any problems with it or – even better – ways to improve upon it, you’ll let me know.

This code goes in an .htaccess file in the root of http://www.mysite.com/. Further up the file, a check has already been made to determine whether or not the user agent is a mobile device and if it is, the value of an environment variable called $IS_MOBILE has been set to “true”. The ErrorDocument for 404 errors has also been set.


# Activate the RewriteEngine
RewriteEngine on

# Set the base to root
RewriteBase /

# Check whether it's a mobile device
RewriteCond %{ENV:IS_MOBILE} ^true$ [NC]

# Check that the URL isn't already going to the mobile version
RewriteCond %{REQUEST_URI} !^/mobile/ [NC]

# Capture the part of the URL that follows after the base, prepend it with "/mobile/" and
# assign the new string to an environment variable called $MOBILE_TARGET_URL;
# Also assign the un-prepended captured value to an environment variable called
# $MOBILE_ORIGINAL_URL; Chain this rule to the next one.
RewriteRule ^(.*)$ - [E=MOBILE_TARGET_URL:/mobile/$1,E=MOBILE_ORIGINAL_URL:$1,C]

# Concatenate the $DOCUMENT_ROOT and $MOBILE_TARGET_URL together
# and check whether that maps to either a directory or a file
RewriteCond %{DOCUMENT_ROOT}%{ENV:MOBILE_TARGET_URL} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:MOBILE_TARGET_URL} -f

# If it does, redirect to the user to the mobile version
RewriteRule ^.*$ %{ENV:MOBILE_TARGET_URL} [R,L]

Dual Eclipse

Friday, 4th June 2010, sheilaellen (http://blogs.bluegumtree.co.uk/vista/)

Usually I code in Eclipse. It saves me a lot of time, especially thanks to plugins such as oXygen and Mylyn.  However, it’s been frustrating me for a while that although I can pop-out individual Views and position them on my second monitor, it didn’t seem possible to open a second instance of the same View. I was wrong!  Just select “New window” from the “Window” menu.  It seems to have full functionality, you can even select an entirely different Perspective, if you wish. Simples! As Aleksandr Orlov would say :)

Aleksandr Orlov

Karmic Eclipse

Saturday, 16th January 2010, sheilaellen (http://blogs.bluegumtree.co.uk/vista/)

After upgrading Ubuntu to Karmic Koala I experienced a few problems getting Eclipse to work properly – mostly buttons that clicked but triggered nothing. Thankfully, Chris Chrisostomu had already published a blog post on the matter, including instructions for a potential fix that certainly seems to have sorted it out for me:
http://mou.me.uk/2009/10/31/fixing-eclipse-in-ubuntu-9-10-karmic-koala/

My thanks go to Chris for writing it up so clearly.