2009-07-30 15:46

In the vast majority of cases, http redirections are installed on the http server.
However, the redirection cannot be installed in the http server config in some cases.

Example: mobile phone redirection.
We want to redirect all mobile phones from http://www.mysite.com to http://mobile.mysite.com

The client type is done with the HTTP User-Agent header sent by the browser. Problem: Squid will put in cache only one version of the query to http://www.mysite.com. The response from the cache will probably be the home page, not the 301 redirection.

It’s also possible to configure the HTTP server to add the Vary: User-Agent header to tell Squid to store one version by browser. With this the cache will be split (one cache per browser), lowering a lot the cache efficiency.

Here is the solution:

###################################
# we redirect mobiles to mobile.mysite.com
url_rewrite_program /etc/squid/redirect_mobile.sh

acl symbian browser Symbian
acl iphone browser iP(hone|od)
acl mobile_url dstdomain mobile.monsite.com

url_rewrite_access deny mobile_url
url_rewrite_access allow symbian
url_rewrite_access allow iphone
url_rewrite_access deny all

And the /etc/squid/redirect_mobile.sh script simply contains:

#!/bin/sh
while read line
do
        echo "301:http://mobile.mysite.com"
done

Notes:

  • The mobile detection method is far from being exhaustive. If you know a simple method covering 95% of browsers, I’m interested!
  • With more recent Squid versions (3, 2.HEAD), you can use a better method using internal redirectors.

Links:

2009-07-30 15:46 · Tags: , ,

2 Comments

  1. Hi,

    I’m stuck with this problem right now. Here’s a more thorough header detection script:

    http://mobiforge.com/developing/story/lightweight-device-detection-php

    Reply

  2. I ended up investigating javascript solutions. It’s not perfect, but this seems pretty good:

    http://blog.sebarmeli.com/2010/11/02/how-to-redirect-your-site-to-a-mobile-version-through-javascript/

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>