There are a number of situations where you may need to redirect the browser based on the user agent of the browser. An example would be to an IE6 page, or a smartphone modified subdomain.

If you load Nick Yeoman dot com on IE6 you will be directed to my IE6 unsupported page. This is how I implemented this functinality on my site.

<?php
/**
* Redirect IE6
* v1.0
* Last Updated: June 5, 2011
* URL: http://www.nickyeoman.com/blog/php/64-php-redirect
**/

$useragent = $_SERVER['HTTP_USER_AGENT']; //get the user agent

if( strpos($useragent,"MSIE 6.0") ) { //string to match

  header("Location: http://nickyeoman.com/ie6"); //where to go

}
?>

First I get the user agent and store it in a variable, pretty straight forward. Next I use strpos.

The most difficult part of the whole process was determining what user agent IE6 uses. I could have just opened IE and echoed the user agent, but I was on a Linux system so I used User Agent String dot com's list.

Gotcha - Infinate loop

If you are using a CMS or a shared template you will want to exclude the actual IE6 page in the if statement. Otherwise an infinate loop will occur. In Joomla I do this:

if(strchr($useragent,"MSIE 6.0") && $articleid != 'article148') {
//php header function goes here
//article id will depend on your install
}

The header() php function will do a standard redirect and you could easily move the user to anywhere on the web, not just your own site. For example here is how I would redirect to a mobile site.

header("Location: http://m.nickyeoman.com/");
//and a new domain would work as well
header("Location: http://nickyeomanMobile.com/");

IE6 Javascript alternative

Although this aritcle is focusing on PHP, if your only worried about IE6 redirection you can use this javascript method.

<!--[if lte IE 6]>
<script type="text/javascript">
window.location = "http://nickyeoman.com/ie6";
</script>

Some claim the php method is called browser sniffing and can harm your SEO rankings, but my research sees no indication of this. Google only cares if you show users different content than Google bot. This method is showing all users the same thing based on user agent.

More examples

This article so far has just explained how to redirect the browser, but you could use this method for other functions, such as loading style sheets or hidding elements. Here is a style sheet example:

<?php

$useragent = $_SERVER['HTTP_USER_AGENT'];

//mobile example
if( strpos($useragent,"Blackberry") ) { 
header("Location: http://m.nickyeoman.com/");
}

//css example
if( strpos($useragent,"wii") ) { ?>

<php } else { ?>

<php } ?>