Conditional redirect

Discussion in 'PHP' started by Ghalt, Apr 8, 2008.

  1. #1
    Is it possible to create an index page that redirects based on data in the referring link?

    For example, at a domain-level (mysite.com), have an index.php that points to "mysecondsite.com" if the word "diet" appears in the referring link, and points to "mythirdsite.com" if the word "weight" appears in the referring link...then defaults to another page if neither condition is met.

    Doable? Difficult?
     
    Ghalt, Apr 8, 2008 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Using php and <META HTTP-EQUIV = 'Refresh' Content = '2; URL =xxxx.com'> along with a $_SERVER["REQUEST_URI" statement which is parsed to extract what you want is should be doable.

    The $_SERVER["REQUEST_URI" can be faked if it is not coming from your server, maybe even if it is, I don't really know for sure.

    Not sure what the SEO implications might be.
     
    Colbyt, Apr 8, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I would do something like this. This would go at the top of the page above any output.

    
    
    $redirect_array = array(
    'health' => 'http://www.healthsite.com',
    'cars' => 'http://www.autos.com'); //add the find terms, and urls in this array
    
    foreach($redirect_array as $find => $redirect)
    {
    
    if(strpos($_SERVER['HTTP_REFERER'],$find) !== false)
    {
    
    header("Location: $redirect");
    exit();
    
    }
    }
    
     
    //default
    
    header("Location: http://www.defaultpage.com");
    exit();
    
    PHP:
    As stated the $_SERVER['HTTP_REFERER'] variable is unreliable, but this code should work for you. If you need a specific redirect method, IE 301, you can put the 301 header above the redirect header.

    Ex:
    
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.defaultpage.com");
    
    PHP:
     
    jestep, Apr 8, 2008 IP
  4. Ghalt

    Ghalt Peon

    Messages:
    182
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    OK...I've got it working. It's similar, but not as clean, as jesteps, however it's easy to understand for non-coding folks.

    In addition, I've figured out how to do it and still have Google Analytics track the visits.

    I've posted a thread on my blog:

    http://www.best-free-information.com/2008/04/conditional-redirect/

    Thanks for the input.

    FYI - the main reason for doing this is so that I can write EzineArticles, and give them one domain name (top-level) to visit. Based on the article they're coming from, I can redirect them to my affiliate instantly.
     
    Ghalt, Apr 15, 2008 IP