Redirect Mobile users

Hi,

I have a website where I want to redirect users on a mobile device from the homepage to a subpage such as /news/.

Here is what I have in my htaccess.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} !/news/.*$ [NC]
RewriteRule .* /news/ [R=302,L]
</IfModule>

When I visit the webpage on a mobile I get an error 310(net::ERR_TOO_MANY_REDIRECTS).

If anyone can shed any light on what I have wrong in my htaccess I’d be grateful.

Thanks,
Jon

Hi,

not sure if this helps much, but the above code works fine for me on an iPhone3, as well as an iPad2.
That is to say, if I copy this into a .htaccess file in my site’s root, I get redirected to mydomain.com/news/ as soon as I hit the site on either of those devices.

I did have an issue recently with a browser (either Chrome or FF) caching redirects, which caused me endless amounts of pain.
Would it maybe help to clear your browsing data?

This also could be accomplished via the PHP code:

<?php
$useragent=$SERVER[‘HTTP_USER_AGENT’];
if(preg_match(‘/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|meego.+mobile|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i’,$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|
)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i’,substr($useragent,0,4)))
header(‘Location: http://yourdomain.com/mobile-site.html’);
?>

(c) detectmobilebrowsers.com

@Pullo Thanks for checking it. I should have mentioned that it is a Wordpress install. I’ve been trying to fix this for so long my brain is not working! I tried that snippet on a normal html page and the htaccess redirect worked so maybe it has problems with the Wordpress rewrite rules.

@euro-space I tried your solution but unfortunately that solution didn’t work. I also tried another PHP based solution but I get the same error as the htaccess. Seems to be stuck in an infinite redirect loop.

I’m stumped!

Thanks again for your help guy’s. I’ll keep trying.

Hi there,

What is exactly in your .htaccess file?

I have a couple of WordPress sites on the go and I added this to the .htaccess file for one of them, and it worked on both the iPhone and iPad.

Here’s the content of my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} !/news/.*$ [NC]
RewriteRule .* /news/ [R=302,L]
</IfModule>

Is yours wildly different?

Hi Pullo,

Here is the full htaccess file.


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} !/news/.*$ [NC]
RewriteRule .* /news/ [R=302,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The only difference I can see is the order of the two rewrites and the comments around the Wordpress section.

I’ll try and copy what you have into my htaccess and see if that works.

Thanks.

Cool.
Let me know how you get on.

All,

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

Okay, on to the code being attempted (DELETE the code in red):


[COLOR="#FF0000"]<IfModule mod_rewrite.c>[/COLOR]
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} [COLOR="#FF0000"]"[/COLOR][SIZE=5][COLOR="#0000FF"]([/COLOR][/SIZE]android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile[SIZE=5][COLOR="#0000FF"])[/COLOR][/SIZE][COLOR="#FF0000"]"[/COLOR] [NC]
RewriteCond %{REQUEST_URI} ![COLOR="#FF0000"]/[/COLOR]news/[COLOR="#FF0000"].*$ [NC][/COLOR]
RewriteRule .* /news/ [[COLOR="#FF0000"]R=302,[/COLOR]L]
[COLOR="#FF0000"]</IfModule>[/COLOR]

# vs???

[COLOR="#FF0000"]<IfModule mod_rewrite.c>[/COLOR]
RewriteEngine On
[COLOR="#FF0000"]RewriteBase /[/COLOR]
[COLOR="#FF0000"]RewriteRule ^index\\.php$ - [L][/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="#FF0000"]</IfModule>[/COLOR]

I make these “corrections” every time I see the WordPress code. Hopefully, one day they will understand mod_rewrite code and I won’t have to repeat this “repetitively.” Please note that WP uses the <IfModule> wrapper to prevent annoying complaints from “webmasters” who do not understand .htaccess use. If mod_rewrite is enabled, it’ll work. If it isn’t, you’ll get a 500 error and can eliminate the mod_rewrite code BECAUSE Apache does not recognize its directives.

To the OP’s problem, I believe that the quotes were in error and a parenthetical was in order (to select one of the set of items within the parentheticals).

Regards,

DK

@Pullo I tried your suggestion but it didn’t work, sadly.

@dklynn First thanks for the help, I will try what you suggest. However, I DO NOT appreciate being called an idiot. I have only been programming in PHP and using linux servers for about a year so I still have a lot to learn. I am here to learn not to have someone rant at me over the internet like a child. These forums are supposed to be somewhere you can ask questions when you need help, not have abuse thrown at you. Not everyone has the knowledge you seem to have so instead of being abusive how about educating the people who are asking for help in a constructive way? You have now been reported.

@dklynn, Thanks for that. I had no idea that using <IfModule> was such a bad practice. I’ve had a look through your mod_rewrite tutorial, too. Very informative!

Pullo,

No worries!

Why do you have a pounamu koru as your avatar? That’s something Kiwis know all about but it is rarely seen elsewhere.

Regards,

DK

The answer is not very exciting, I’m afraid: I was looking for an avatar, I liked the design of this one and it was free for personal use.
It was indeed entitled ‘pounamu koru’ which struck me as a strange name, so I googled it and read up on its significance.
I thought twice about whether to use it or not, but decided that it was unlikely that anyone would ask me about it … :slight_smile:

Pullo,

You’ve got me on that one as I’d not seen that as an available avatar before. Frankly, it looks GOOD and I was rather shocked to see it here. I thought either you were a misplaced Kiwi or had been one of NZ’s many holiday visitors and had picked-up some local handcrafted items like this jade circle of live. Two :tup: :tup:

jon,

I was not calling you an idiot so my apologies for not making that clear. I do, however, have problems with “webmasters” who blindly use code whose function is unknown to them (IMHO, that’s unprofessional). I have made that comment too many times over too many years to repeat it at length every time I see that <IfModule> nonsense in .htaccess code that I had to create a Standard Rant to prevent Carpal Tunnel Syndrome. My series of rants have been useful to many others (e.g., Pullo above) who have taken them the way they were intended (an instructive “wake-up call”) rather than as a personal insult. I do not consider it useful to explain about CTS prevention in the rants nor about why “Standard Rants” were deemed necessary in the first place. As for my being able to educate people, I’ve been doing that far longer than … well, let’s say that I’ve helped many, many people over too many years to admit to. One or two others have initially taken mild offense but understood that my comments were not personal and used the various rants to help them learn and remember. I hope you will, too.

Regards,

DK

DK, Sorry for my over reaction. I guess I am just used to forums full of trolls who call other people stupid etc. if they don’t something. I understand your frustrations at repeating yourself but as I mentioned above I have not been coding PHP and using Linux servers long (I’m a front end developer by trade trying to get more strings to my bow by learning PHP, ASP etc). Before posting in this forum I searched Google for a solution which is where I got my code shown in the first post so I guess there are many websites out there with the wrong information.

Thanks for your help too btw, I tried what you suggested and it worked perfectly. I have bookmarked your website and will use for future reference as I continue learning PHP and Linux servers.