473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Mobile Browser detection and redirection

HaLo2FrEeEk
404 256MB
Ok, I've done a little research on this subject and I've come up with 2 good methods. The first uses a PHP script that detects the $_SERVER['HTTP_USER_AGENT'] variable and parses it for information that might indicate that the browser is a mobile one. This works well, I tested it with my Motorola Droid with Android 2.0.1 and it successfully reported not only that I was on a mobile browser, but also that I was on an android device.

However, I learned about another way, using a similar concept. Mod_Rewrite. I've done some searching and uncovered a few mod rewrite commands that will redirect a mobile browser, but I haven't tried any of them, mostly because I've got other RewriteRules already present on my site.

My main question is what is going to be the best method? Logic leans toward Mod_Rewrite because it occurs before the page is even loaded, meaning I won't have to even run the PHP script in the first place.

My second question, if I do go with Mod_Rewrite, will the RewriteCond's and RewriteRules interfere with the ones I already have implemented? The ones I have right now are things to redirect from subfolders to subdomains where I've moved files to keep organized, or to rewrite dynamic pages with parameters to static-looking pages.

I don't want those to be disrupted, but I want all mobile browsers to be redirected to the mobile version of the site.

Can someone help me out?

I should also note that I'm not very good at the Regular Expressions used in Mod Rewrite, so I might need help with that, too.
Jan 22 '10 #1
3 5420
Atli
5,058 Expert 4TB
Hey.

RewriteRules are executed in a sequence, just as any code, so if you place your rule for redirecting mobiles in front of the others, and then stop the execution, it should not interfere with your old rules.

For instance, something like this should be perfectly fine:
(Note, this is just an example. I didn't test it.)
Expand|Select|Wrap|Line Numbers
  1. # Silently redirect IE to another version of the site
  2. RewriteCond %{HTTP_USER_AGENT} MSIE
  3. RewriteRule ^(.*)$ broken.php?$1 [L,NC]
  4.  
  5. # Redirect Android somewhere else
  6. RewriteCond %{HTTP_USER_AGENT} Android
  7. RewriteRule ^(.*)$ android.php?$1 [R=301,L,NC]
  8.  
  9. # And put your old redirects here
  10. RewriteRule ^search/([^/]*)/?$ search.php?keyword=$1 [L]
  11. RewriteRule ^blog/(\d+)/? blog.php?id=$1 [L]
  12. # etc...
Also, just to put it out there...
I would be careful in redirecting all mobile browsers from your main site. Some of them may well prefer to view the standard site rather than the mobile one.
Personally, I would consider it a better tactic to detect mobile browsers using PHP, and add a sort of splash-screen, offering them a link to the mobile version.
Jan 22 '10 #2
HaLo2FrEeEk
404 256MB
I fully plan to redirect all detectable mobile browsers to the mobile site, then provide a link to the full version if the user so desires. That's another thing, how would I go about allowing the user back into the mobile site if I'm doing a redirect? Cookies?
Jan 22 '10 #3
mlynn
1
I offer the following article in answer to your question... I created it to show an example of how to redirect users based on their device / browser to unique instances of your web content. The example also includes a mechanism for allowing users to specify a preference for / view other site versions.

http://www.mlynn.org/2010/06/mobile-...tion-with-php/

Enjoy!
Jul 9 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stephan | last post by:
Hello to all, I'm looking into providing mobile access to a web based administrative application currently running on .Net with MS SQL. The current architecture only supports the latest Internet...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
1
by: Phil Townsend | last post by:
How does one detect for Internet Explorer on Windows CE? Does Request.Browser.Browser return anything unique for mobile versions of IE? I have a strange situation where I need to develop a user...
0
by: Carlos | last post by:
Hi all, I am currently able to detect the browser capabilities, and determine if a browser is from a mobile device. However, I only have been able to place this detection code in an aspx page....
9
by: SHarris | last post by:
Hello, In our new intranet ASP.NET project, two requirements are that the browser accept cookies AND JavaScript. We are requiring the use of Internet Explorer 6+. 1. Using C# in an ASP.NET...
1
by: jaydev | last post by:
Hi, I am looking for code to detect and redirect to the corresponding browser download page if the clients uses old version, could anyone have a sample code for this? I want to use the...
4
by: igotyourdotnet | last post by:
I have a web app that I want to make the pages render correctly based on what type of browser is connected. How can I determine if the browser is the desktop IE version or a handheld device version?
3
by: quushich | last post by:
Hi all... I need some help figuring out what sort of script is needed to first: detect if a viewer is coming from a mobile device and second: if so, redirect the viewer to the mobile site. IIS...
1
by: Sleepwalker | last post by:
Hi, I'm currently in the process of constructing a stripped down version of the company website that will be show to people logging in from a mobile device. When the user logs in to the homepage,...
1
by: =?Utf-8?B?V29vZGdub21l?= | last post by:
Hi, I have use the following code to transmit a file back to the browser that worked fine on a HTC_TTyN device running windows mobile 5.0. The code executes in the onclick event of a mobilelist...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.