Forum Moderators: phranque

Message Too Old, No Replies

redirecting iphones to mobile site

But want to allow option to use the main site

         

lokeshshettyk

7:12 am on Nov 21, 2008 (gmt 0)

10+ Year Member



Hi,

I have the following redirect set-up for my iphone site. If anyone trying to go to [sub.example.com...] on an iPhone/iPod that redirects them to [iphone.sub.example.com...] - this works all fine :)
Here is the code for the above


RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari [NC]
RewriteCond %{HTTP_HOST} !^i\.
RewriteRule (.*) http://iphone.sub.example.com/$1 [R=303,L]

Now i have a link on my iphone site to go to the main site (http://sub.example.com/). But when i click on this, since I am on an iPhone/iPod it takes me back to [iphone.sub.example.com...] instead of [sub.example.com...] How do i fix this up?

Please help.

Samizdata

10:19 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As far as I am aware iPods include "iPhone" in the user-agent.

Note that Google Android includes both "Mobile" and "Safari" in the string.

I would probably start with something like this:

RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{HTTP_REFERER} !^http://(iphone\.)?sub\.example\.com [NC]
RewriteRule /*$ [iphone.sub.example.com...] [R=301,L]

Untested, but might be useful to you.

...

lokeshshettyk

10:50 am on Nov 21, 2008 (gmt 0)

10+ Year Member



I have no problem with the redirect. The problem is with my second paragraph. Once i enter [iphone.sub.example.com...] i have a footer link which is hard coded (http://sub.example.com). Onclick of this link it should go back to my main site (http://sub.example.com) and it does not since my initial redirect condition sends me back to [iphone.sub.example.com...]

Cheers :)

jdMorgan

2:22 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to set a cookie when the "go to main site" link is clicked, and test that cookie on the main site.

If the cookie is set, then do not redirect to the mobile site. If the cookie is not set or if it indicates that the user is viewing the mobile site, and the user-agent indicates a mobile device, then redirect to the mobile site.

The cookie can be a browser-session cookie that expires when the browser is closed, or you can ask the user how long he/she wants to use the main site, and set a cookie for that time period, from one hour to 5 years (which is "forever" in terms of a mobile device's life).

You will need a server-side script to do the cookie-creation stuff, as it isn't something that can be handled within Apache itself; Cookie expiration date calculations are the difficult part, but there are libraries in PERL and PHP to support that.

Apache mod_rewrite can test the cookie, once you've created it:


RewriteCond %{HTTP_USER_AGENT} Mobile.*Safari [NC]
RewriteCond %{HTTP_HOST} !^i\.
RewriteCond %{HTTP_COOKIE} ^(view=mobile)?$ [NC]
RewriteRule (.*) http://iphone.sub.example.com/$1 [R=303,L]

The cookie-setting logic implied here is:
If the cookie is blank or set to "view=mobile" and the request is not for a subdomain starting with "i.", and the user-agent indicates mobile safari, then redirect to the mobile site.
On the mobile site, set the cookie to "view=mobile" for all mobile page requests.
If the user clicks to go to the non-mobile site, set the cookie to "view=web".

Now, if the user wants to go back to the mobile site he/she can, but it will set the cookie back to mobile view mode. So, the "web view" mode is "sticky" for the browser session or for the user-selected time, but if they click a link to the mobile site or enter its URL directly, then the cookie will be set back to mobile mode.

The view=web and view=mobile values are just made-up; You can anme the cookie anything you like, and use any values you like to designate mobile- versus non-mobile viewing.

I hope this is clear. It's fairly simple, but a bit difficult to explain without being able to draw a state diagram showing the cookie value and mobile/non-mobile domain states and transitions...

Also, this example is just one way you could do it; Its advantage is that it seems like the simplest approach, but it may not be the best method for your site.

Jim

lokeshshettyk

8:23 am on Nov 23, 2008 (gmt 0)

10+ Year Member



I would say ' wow'. amazing thought Jim. Thanks a ton. I would try this and get back to you with the result. Thanks again for your help.

Cheers ;)

ifushini

4:20 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



Hi,
I read only now this post, I realize an Apache filter that can do more sophisticated redirect for mobile solution.

<snip>

[edited by: jdMorgan at 4:23 pm (utc) on Jan. 20, 2009]
[edit reason] No URLs, please. See Terms of Service. [/edit]