php redirect based on referrer

Discussion in 'Search Engine Optimization' started by drig, Feb 27, 2008.

  1. #1
    Hi,
    Does anyone know how to do a php redirect based on referrer?

    E.g.: someone comes from outsidesite.com to mysite.com, but I want to redirect them to mysite1.com.

    Thanks,
    Dave
     
    drig, Feb 27, 2008 IP
  2. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try:

    Havn't tested it just knocked it together, but it should direct referrers from outsidesite.com to mysite1.com and all other referrers to plain mysite.com
     
    sweetfunny, Feb 27, 2008 IP
  3. charroseo

    charroseo Peon

    Messages:
    293
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can also try JS redirect... just another option:


    <script language="JavaScript"><!--
    if (document.referrer.indexOf('outsidesite.com') > -1)
    location.href='http://mysite1.com';
    else
    location.href='http://mysite.com';
    //--></script>
     
    charroseo, Feb 28, 2008 IP
  4. Ghalt

    Ghalt Peon

    Messages:
    182
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    For the PHP solution, what would the syntax be to search for a substring within the referer?

    In other words, any referer with "dog" in the url gets redirected to www.dogtrainingaffiliate.com and any referer with "work_from_home" gets redirected to www.wfhaffiliate.com, etc., else redirect to a page within my site.
     
    Ghalt, Apr 9, 2008 IP
  5. budhanes

    budhanes Peon

    Messages:
    245
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You coud do a search on "PHP regular expressions" and probably find a solution. I use PHP on occassion, but I'm not very fluent in regualr expressions. I grabbed this link real quick to help guide you...

    http://www.regular-expressions.info/php.html

    Quoted from this site:

    preg_match (string pattern, string subject [, array groups]) returns TRUE if the regular expression pattern matches the subject string or part of the subject string.
     
    budhanes, Apr 10, 2008 IP
  6. oneg

    oneg Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("/outsidesite.com/",$referrer)) {
    header('Location: http://www.mysite1.com');
    }
    else {
    header('Location: http://www.mysite.com');
    };
    ?>


    If I want to match a LIST OF SITES, insted of "outsidesite.com"
    How can I work with a txt list of sites: "mysites.txt"
    instead of only one site: "outsidesite.com"

    <?
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("(HERE A TXT WITH A LIST OF SITES",$referrer)) {
    header('Location: http://www.mysite1.com');
    }
    else {
    header('Location: ht*tp://www.mysite.com');
    };
    ?>


    Anyone knows how to do it?
     
    oneg, May 12, 2008 IP