Forum Moderators: phranque
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.
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.
...
Cheers :)
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]
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