Jump to content

Redirecting mobile browsers


HaLo2FrEeEk

Recommended Posts

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.

Link to comment
Share on other sites

Well, thank you for the reply, but I'm disinclined to take advice with a caveat of "I don't see what could go wrong."

 

I've heard advice now telling me to put it at the beginning, before my main site's rewriterules, and you telling me to put it at the end.

 

And I'll still need help with the rewritecond part.

Link to comment
Share on other sites

Well, thank you for the reply, but I'm disinclined to take advice with a caveat of "I don't see what could go wrong."

 

I've heard advice now telling me to put it at the beginning, before my main site's rewriterules, and you telling me to put it at the end.

 

And I'll still need help with the rewritecond part.

 

Are you scared to test it out?

 

mod_rewrite rules won't matter as they parse before acted upon, If you want to put it at the beginning go ahead, rules are rules for a reason, It's not like you have to put them in any order * unless there is a rewrite condition attatched.

 

Mod_Rewrite because it occurs before the page is even loaded

 

All that does is pass the directive over to apache, Not a good idea if you aren't writing efficient rules as it'll slow PHP's bind. Btw, it compiles via symlinks as PHP would in ZEND. What is the difference in speed?

 

It'd help if you showed some code to help you on, What rewrite directives are you using, and what are your current directives so we can tell incompatabilities? All you'll get is a temporary 500 server error until you revert the mod_rewrite if it goes wrong, which takes seconds, wasting time by not helping us or yourself.

Link to comment
Share on other sites

Using mod rewrite for redirection is fine. It doesn't matter where you place the lines in your htaccess file! I would post them on the mod rewrite forum board if they are not working correctly.

 

The main disadvantage of using a rewrite rule is that a mobile user will always be redirected whether they wish to or not. If I was using an iPhone I may still want to view your main website rather than a cutdown mobile version. Using a php script to handle the redirection you would be able to offer users the choice of which site to view. i.e

 

1. Detect user agent

2. If mobile device and no preference cookie is set redirect user to choice screen

3. If user wants to be on mobile site or main site set a cookie for this option

4. Redirect user

5. If cookie is set to mobile and user lands on main website then redirect

Link to comment
Share on other sites

Can you gave a code snippet for following ?

 

1. Detect user agent

2. If mobile device and no preference cookie is set redirect user to choice screen

3. If user wants to be on mobile site or main site set a cookie for this option

4. Redirect user

5. If cookie is set to mobile and user lands on main website then redirect

Link to comment
Share on other sites

Can you gave a code snippet for following ?

You would have something along the lines of the following run in a common include of all pages of your site. The function would have to be completed by yourself aswell as the setting cookies, etc, that is fairly straightforward

<?php
function isMobileDevice($agent) {
// detect a mobile device
if() {
	// is a mobile device
	return true;	
}
// isnt a mobile device
return false;
}

if(isMobileDevice($_SERVER['HTTP_USER_AGENT'])) {
if(!strlen($_COOKIE['agentpref'])) {
	// redirect user to make a choice of which site to view
	header("Location:site-choice.php");
	exit();
}
else {
	// redirect to mobile site
	if($_COOKIE['agentpref'] == 'mobile') {
		header("Location:http://mobile.xyz.com");
		exit();	
	}
}
}
?>

Link to comment
Share on other sites

Will the above code automatically detect the mobile browser?

 

Why not read the code? He filled out the shell of what is needed. All that is required is to fill in the if() that the OP wanted within. (In case he goes the PHP way)

Link to comment
Share on other sites

I have the library that that site offers, it's free if the site it will be used on is non-profit or doesn't show ads, and either way how are they going to know?

 

Anyway, it looks like PHP is going to be the way to go.  I was looking for a way to give the users a choice, I thought mod rewrite can access cookies though, can't it?

 

HTTP_COOKIE

 

right?  Is PHP just simpler to use in this case, or will mod_rewrite really not work?

 

I'm not arguing against the effectiveness of PHP, it would definitely be nice to use something I understand.  I just have an idea.  I could automatically redirect all mobile browsers to the mobile version of the site using mod_rewrite if a cookie is not present, then provide a link to take the person back to the full version.  This link would go to a redirection page which would set the cookie, then the next time the person went to the site, the mod_rewrite would read the cookie and determine if the user wanted the mobile version or full version.  That would work too, right?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.