PHP Redirect By Referrer

Discussion in 'PHP' started by ravenplus, Feb 4, 2009.

  1. #1
    Looking to redirect from referrer to a different page on my website.

    For example:

    I have links posted on digg.com going to http://mywebsite.com

    I would like to have these visitors go to http://mywebsite.com/newpage.html


    I've searched google on this, .htaccess, javascript, and php, but all the code's in forums I found don't work for me as I believe they are doing more complex redirects then what I'm trying to do.

    Anyone know the proper way to do a PHP redirect based on referrer?
     
    ravenplus, Feb 4, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can simple
    if (strpos($_SERVER['HTTP_REFERER'], 'digg.com')) header('Location: http://mywebsite.com/newpage.html');
    PHP:
    do the job?
     
    steelaz, Feb 4, 2009 IP
  3. ravenplus

    ravenplus Active Member

    Messages:
    472
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Yes that did work, but now I get this error when trying to view my domain from other referrer or by just type in.

    and is this the proper code

     
    ravenplus, Feb 4, 2009 IP
  4. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
    The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

    Please REPLACE domain.com and www.newdomain.com with your actual domain name.

    Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

    or

    PHP Redirect

    <?
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: http://www.new-url.com" );
    ?>

    Point change

    http://mywebsite.com/
    to
    http://www. mywebsite.com/

    in htaccess as well as SE will see this as different domains

    The first example will work but ensure you have mod_rewrite enable on your server
     
    swapshop, Feb 4, 2009 IP
  5. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You should check your other code (or paste it in here), as the that line cannot through you into redirect loop unless you include it in newpage.html and your HTTP_REFERER string has 'digg.com' in it.
     
    steelaz, Feb 4, 2009 IP
  6. ravenplus

    ravenplus Active Member

    Messages:
    472
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #6

    What "other code" are you referring to?
     
    ravenplus, Feb 5, 2009 IP
  7. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So your page contains only this one line of code? To debug you can replace it with:

    <?php if (strpos($_SERVER['HTTP_REFERER'], 'digg.com')) echo "will redirect"; ?>
    PHP:
    and see what happens when you access it using different referers or no referer.
     
    steelaz, Feb 5, 2009 IP
  8. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #8
    For Wordpress Users WPTrafficTools is an inexpensive alternative to manually coding the php detection profiles in. 
     
    adbox, Aug 2, 2011 IP
  9. freelanceinphp

    freelanceinphp Member

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #9
    @ravenplus, I recommend to write exit(); after header statement. let me know if you have still redirect problem
     
    Last edited: Aug 3, 2011
    freelanceinphp, Aug 3, 2011 IP