SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    <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

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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?

  3. #3
    Non-Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code:
    # 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?

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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:

    Code:
    # 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.

    Code:
    <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.

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Cool.
    Let me know how you get on.

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    All,

    [rant #4]
    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).
    [/rant 4]

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

    Code:
    <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>
    
    # vs???
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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.

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by dklynn View Post
    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).
    @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!

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  12. #12
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by dklynn View Post
    Why do you have a pounamu koru as your avatar? That's something Kiwis know all about but it is rarely seen elsewhere.
    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 ...

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Pullo View Post
    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 ...
    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

    Quote Originally Posted by jonpenny View Post
    @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.
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •