Redirect Based On Browser (user agents)

Last Updated: Jul. 18th 2022 at 12:23am Tags: blog php

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.

* Redirect IE6
* v1.0
* Last Updated: June 5, 2011
* URL:

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

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

  header("Location:"); //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.

//and a new domain would work as well

IE6 Javascript alternative

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

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:


$useragent = $_SERVER['HTTP_USER_AGENT'];

//mobile example
if( strpos($useragent,"Blackberry") ) {

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



You need to login to comment.