How To: PHP Redirects

We get a lot of questions about using the MobileESP project code in practice. Here’s one example on how to redirect browser traffic to optimized content.

In this scenario, we put a PHP file in the root of a web site (for example, wwww.yoursite.com/index.php), but it could be any folder or any page, as well. To keep this scenario simple, we redirect all traffic like so:

  1. iPhone Tier devices and iPads: Redirect to www.yoursite.com/i/
  2. Rich CSS devices: Redirect to www.yoursite.com/r/
  3. Generic mobile devices: redirect to www.yoursite.com/m/
  4. Desktop browsers and all others: www.yoursite.com/d/

So this is how we would write the code to achieve these goals within the index.php file. This would be the complete contents of the file:

<?php
include("/folder/mdetect.php");
//Instantiate the object to do our testing with.
$uagent_obj = new uagent_info();
//Detect iPhone Tier and iPads...
if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) ||
($uagent_obj->DetectIpad() == $uagent_obj->true))
   { header('Location: http://www.yoursite.com/i/'); }
//Detect Rich CSS Tier...
else if ($uagent_obj->DetectTierRichCss() == $uagent_obj->true)
   { header('Location: http://www.yoursite.com/r/'); }
//Detect All Other Mobile Devices...
else if ($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true)
   { header('Location: http://www.yoursite.com/m/'); }
//Else it's a regular PC browser -- send to regular desktop site
else
   { header('Location: http://www.yoursite.com/d/'); }
?>

That’s pretty much it! This basic strategy can be used in many ways in addition to redirects.

Owner of the MobileESP project. For the latest source code downloads, visit: https://github.com/ahand/mobileesp

Posted in How To - PHP
22 comments on “How To: PHP Redirects
  1. Brent Whinfield says:

    Thank you very much for this great info, Mr. Hand.
    Much obliged!
    Regards,
    Brent.

  2. anthony says:

    I tried the above to redirect and I get the following error when I run the page

    Parse error: syntax error, unexpected ‘{‘ in /mobiletest.php on line 26

    Any thoughts?

  3. Anthony Hand says:

    Hi, Anthony —

    Your file’s name is “mobiletest.php”? That looks like a file that you created to keep your detection logic.

    That error sounds like you’ve added an extra ‘{‘ in your code. So review your code with a very close eye.

    – Anthony

  4. Anthony says:

    Anthony

    Thanks for such a fast response. I tried again. I took the exact code from this page and put it into the head of my index.php page and put mdetect.php in the same directory and I still get the same error. Any other thoughts would be greatly appreciated.

    Anthony

  5. zurie says:

    same with me…

    same error. line 16

    Parse error: syntax error, unexpected ‘{‘ in /home/zurie/public_html/index.php on line 16

    I know how to copy and paste… check your code in the example? if i find an answer i’ll reply

  6. zurie says:

    Got it… TYPO on your CODE!

    if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) ||

    ($uagent_obj->DetectIpad() == $uagent_obj->true)

    This should have a ‘)’ on the end of true… ))

    like this:

    if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) ||

    ($uagent_obj->DetectIpad() == $uagent_obj->true))

  7. Anthony Hand says:

    Yep! It needed an extra ‘)’. Thank you for solving this one, Zurie!

  8. Crowbaby says:

    This is awesome. I am designer and front end developer. So, I am about to ask a noob question.

    Currently, this works for me. I redirect mobile users to a http://www.mysite.com/mobile/ folder and I redirect desktop users to a http://www.mysite.com/desktop/ folder. However, I really don’t want to redirect desktop users. I want them to remain at the root level (www.mysite.com). Is there any way to do this?

    Thank you. Please advise.

  9. Anthony Hand says:

    @Crowbaby – It’s all in how you set up your If/Then statements. Basically, just do something like this:

    If DetectMobile() is true, then redirect to the mobile site;
    Else, do nothing.

    Because the DetectMobile() methods only return true IF a mobile device is detected, then by letting all else pass — which could be desktops or other types of devices you don’t care about or don’t know about — you’re making the “Else” statement get the default behavior. Simple logic.

  10. vanieg says:

    I am having similar problem as @Crowbaby. I paste the code in my index file and it works great in redirecting to pages on iphones. However I get a

    “Too many redirects occured trying to open “mysite” . This might occur if you open a page that is redirected to open another page which then is redirected to open the original page”

    when using Safari on my Apple Computer. It also stalls in Internet Explorer. It will not open at the root level for desktop use. I am not too good with php (amateur really) but I trying. I humbly request your assistance in getting it to remain at the index.html using the else statement as suggested above for desktop users. Warmest regards.

  11. vanieg says:

    Everything thing now seems fine when I deleted the last else statement.

  12. Ryan Burnett says:

    This site is really helpful. If you have a chance, can you please make a “How To – Javascript” similar to this post? Thank you!

  13. Daniel says:

    Hi Anthony, I’m not familiar with PHP and programming in general so I’m not getting this to work.

    I have a website in flash and I want people to see the HTML version if accessed by any mobile devices.

    This is all I’m doing.

    – Created a file in the root folder called index.php and copied and pasted the code on this page. The only things I’ve changed are the 3 links:

    { header(‘Location: http://www.yoursite.com/r/‘); }
    To
    { header(‘Location: http://www.youtube.com/‘); }

    – Created a folder called ‘folder’ in the root folder and I’ve created another php file called mdetect.php. Within this file I copied and pasted the code I found on this page without changing anything:

    http://code.google.com/p/mobileesp/source/browse/PHP/mdetect.php?r=100

    – I Didn’t do anything else. Should I put anything on the head tags of my index.html page maybe?

    When I try to access my page from my BB and Ipod the redirect to youtube (just for testing) doesn’t accur and I get a blank page as flash is not visible on most mobile devices.

    I’m sure I’m missing something very simple but as I said my knowledege of php is basically zero…

    Thank you in advance for your help.

    Daniel

    PS maybe I’m asking too much but is there anywhere I can download folders/files so to see how they should look within the root folder?

  14. Daniel says:

    Hi,

    Unfortunately I did not get a reply on my previous comment and I cannot even see it posted on the site.

    Can you please tell me if this script workds with PHP Server Vers 5.2.6

    Thank you

    D.

  15. Ray says:

    When I try to run the script, an error saying ‘Fatal error’ Class uagent_info not found
    Not sure why am a noob at php!

  16. mike says:

    hi i am trying to use this script. i am a nube. I am intrested in learning more as i created my site and wish to make a more simple version for phones n such.

    i host with godaday on linix economy which lets me use php.
    i setup a test section and tryed to explain in the test what i want to do.
    it keeps erroring.
    test site=
    http://www.atowntaxi.ca/php/index.php

    Warning: Cannot modify header information – headers already sent by (output started at /home/content/08/7429208/html/php/index.php:3) in /home/content/08/7429208/html/php/index.php on line 35

    this is on a browser and on my phone its a different line. 23 which is windows 7.

    any ideas ? thankyou for your time. if we can get this to work it will be awsome. mike

  17. mike says:

    i got it to work i researched the error had to do with the way i pasted it into the file and created blank space after the closeing php tag. 😀 this is such a cool script i will donate soon 😀

  18. Jikey Wee says:

    below is my php file…
    it works fine for all iphone n desktop
    but it just can’t redirect to this link http://get2online.com/mobile.html
    if i using other mobile device… it keep go to http://get2online.com/apple.html

    you can test it with this link see what happen …
    http://get2online.com/test.php

    DetectTierIphone() == $uagent_obj->true) ||
    ($uagent_obj->DetectIpad() == $uagent_obj->true))
    { header(‘Location: http://get2online.com/apple.html‘); }
    //Detect Rich CSS Tier…
    else if ($uagent_obj->DetectTierRichCss() == $uagent_obj->true)
    { header(‘Location: http://get2online.com/mobile.html‘); }
    //Detect All Other Mobile Devices…
    else if ($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true)
    { header(‘Location: http://get2online.com/mobile.html‘); }
    //Else it’s a regular PC browser — send to regular desktop site
    else
    print( “This is not mobile version”);
    exit;

    ?>

  19. Darren says:

    This method works great (after a little tweaking) – I use it for http://www.digisolved.com and it works great!

  20. John says:

    Hi there,

    Firstly thanks for this excellent script.

    I have the PHP version running but I get into a ‘looping’ issue when the ‘visit full site’ link is clicked from the mobile friendly page as it is redirected once again by the redirect code that sits on the index.php page. I guess this is a sessions or cookies issue but I can’t seem to find the answer on this site. In other words, I am unable to re-visit the full site’s home page without always triggering the redirect script. I hope that makes sense.

    Is there something that I’m missing?

    Thanks in advance.

    John

  21. Scoopy says:

    Awesome!!! Just what I was after. http://detectmobilebrowsers.mobi/ was too expensive for what I was wanting to do.

    I do have a question however: I have a client that only want to allow iphones, and tablets (ipads and galaxy’s) to go to the main site, but everything else, including androids, to be redirected.

    What’s the easiest way to do this? Can I make the change in the index.php or does it have to be in the mdetect.php. If it’s in the mdetect.php what do I need to change?

    Any help would be appreciated.

  22. Kate says:

    Hi. Can you add an example of how to allow access to the full site from mobile site. This should obviously be done by cookies but i cant find any examples for that. Thanks for a great mobile esp!

Leave a Reply

Your email address will not be published.