Need help with redirect script...

Discussion in 'Programming' started by moe65, Nov 1, 2008.

  1. #1
    My website has incoming affiliate links that apend an affiliate reference and a string of numbers to the end of any regular URL for our site. We need to do a 301 redirection after the URL is loaded that will strip off all of the affiliate references and redirect the user to the standard page. (We are seeing google index both the regular and affiliate referenced pages resulting in duplicate content issues which is the reason for the redirect). In any case I came across a script that does half of what we need and it works well when placed in our header:

    <?php
    if (isset($_GET["avad"])) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: url");
    header("Expires: 0");
    exit;
    }  ?>
    Code (markup):

    The problem with this is that it redirects all incoming links with the affiliate reference to our homepage, however many of our affiliate links point to specific product pages and we need to keep those URLs in tact. Does anyone know how we could rewrite this redirect so that it just strips off the affiliate references and load the standard page?

    So for example:

    website.com/?ref=1234567890
    301 redirects to
    website.com
    or
    website.com/product-1.php/?ref=1234567890
    301 redirects to
    website.com/product-1.php


    I don't think we can do this in .htacess because that will mess with the affiliate cookies which we want to keep in tact (however if I'm wrong on that let me know).

    Thanks in advance for any help you can offer!
     
    moe65, Nov 1, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hope you don't mean it literally like quoted above: headers don't work after content has been served ;)

    You're right about the .htaccess/cookies part.

    Do a preg_replace on the URL do get rid of the affiliate id, then do the redirect.
     
    keyaa, Nov 2, 2008 IP
  3. moe65

    moe65 Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Sorry about that, I didn't mean after the URL is loaded (it was late and too much time spent looking up solutions). I looked at the link you sent but couldn't figure out the right syntax to use. Basically we have a URL with ?ref= and we want to strip of that and anything after it (leaving everything before that in tact). I have been looking at the different preg replace statements all morning and can't get it to work right. If anyone can help with the syntax, I'd be very grateful.
     
    moe65, Nov 2, 2008 IP
  4. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $url = preg_replace('/\?ref=.*/', '', $url);
    PHP:
     
    keyaa, Nov 2, 2008 IP
  5. moe65

    moe65 Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Hmm.... still not working.... pulling out hair...
     
    moe65, Nov 3, 2008 IP
  6. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #6
    you have a general header included in all php scripts?

    try this
    <?php
    if (isset($_GET["avad"])) {
    .....set the Aff.-Cookie here.....
    header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]");exit;}
    }  ?>
    Code (markup):
    so if /product-1.php is including the header.php with above content it would work.

    then you call the product like wwww.yoursite.com/product-1.php?avad=myAFFid and it will redirect instantly to wwww.yoursite.com/product-1.php

    enjoy.
     
    happpy, Nov 3, 2008 IP