1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

redirect based on referer

Discussion in 'General Scripting Chat' started by bigfred, Sep 21, 2010.

  1. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    I need to redirect traffic from google.co.uk to a new site.

    I have been told that this is called a redirect based on referer (HTTP_REFERER).

    Can anyone advise how I actually do this? Is it just code in a txt document?

    Thanks in advance guys, I am not a programmer and appreciate your help
     
  2. Pete Thong

    Pete Thong Junior Member

    Joined:
    Nov 19, 2009
    Messages:
    108
    Likes Received:
    36
    First you need to put a file called .htaccess in the directory which should be redirect, e.g. your root directory if you want to redirect the whole domain.

    then open the file with a text editor and add the following lines:

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^http://www\.google\.co.uk [NC]
    RewriteRule ^(.*)$ http://www.yournewdomain.com/ [R,L]
     
  3. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    Hi great thanks. What is the [NC] and [R,L]?
     
  4. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    I have used the rewite you suggested. When I search using google.co.uk it redirects fine, however when I search using this location search:

    google.com/search?q=keyword&gl=uk

    It doesnt redirect :(

    Basically I want anyone who clicks the link in the location UK, then it will redirect. Is this possible?
     
  5. Pete Thong

    Pete Thong Junior Member

    Joined:
    Nov 19, 2009
    Messages:
    108
    Likes Received:
    36
    Do you want to redirect all UK traffic, no matter from which source it comes, or just UK traffic that comes from google?
     
  6. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    I want all UK traffic from Google to be redirected
     
  7. homenet

    homenet Power Member

    Joined:
    Jan 5, 2009
    Messages:
    790
    Likes Received:
    338
    Location:
    Dimension X
    Try adding this line into .htaccess

    RewriteCond %{http_referer} gl=uk [NC]
     
  8. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    tried it, didnt work :(
     
  9. s0phia

    s0phia Regular Member

    Joined:
    Nov 11, 2008
    Messages:
    266
    Likes Received:
    280
    that should do it i believe:

    Code:
    <?php
    
      if (preg_match('/google\.co\.uk/' , $_SERVER["HTTP_REFERER"])) {
        header("Location: http://blackhatworld.com");
        exit;
      }
    
    ?>
    
     
  10. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    hey? :s
     
  11. s0phia

    s0phia Regular Member

    Joined:
    Nov 11, 2008
    Messages:
    266
    Likes Received:
    280
    hey :wave:
     
  12. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    thanks homenet for your help mate. Any other ideas?
     
  13. homenet

    homenet Power Member

    Joined:
    Jan 5, 2009
    Messages:
    790
    Likes Received:
    338
    Location:
    Dimension X
    Try this in your .htaccess file, delete everything that is in it and then put:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} gl=uk [NC]
    RewriteRule /*$ http://www.yoursite.com/webpage.html [R=302,L]

    Then delete your cache and try again.
     
    • Thanks Thanks x 1
  14. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    You are a superstar! It worked! Cheers mate appreciate all your time and help
     
  15. homenet

    homenet Power Member

    Joined:
    Jan 5, 2009
    Messages:
    790
    Likes Received:
    338
    Location:
    Dimension X
    No probs, glad it worked :)
     
  16. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    Quick question, if I want to add more countries would it look like this:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} gl=uk [NC]
    RewriteCond %{HTTP_REFERER} gl=za [NC]
    RewriteCond %{HTTP_REFERER} gl=sg [NC]
    RewriteRule /*$ http://www.yoursite.com/webpage.html [R=302,L]

    ?
     
  17. Griffin

    Griffin Regular Member

    Joined:
    Feb 17, 2009
    Messages:
    205
    Likes Received:
    50
    I'd say PHP is your best bet here, as you can change it page by page if need be. Just my $0.02
     
  18. bigfred

    bigfred Power Member

    Joined:
    Mar 15, 2009
    Messages:
    743
    Likes Received:
    143
    any idaes of how to do it via php?
     
  19. lizmoz

    lizmoz Power Member

    Joined:
    Oct 10, 2008
    Messages:
    561
    Likes Received:
    329
    Yap, this should work. Did you try?

    Code:
    http://www.electrictoolbox.com/php-http-referer-variable/
    You'll get the referer that way.. What's the problem still?
     
  20. Griffin

    Griffin Regular Member

    Joined:
    Feb 17, 2009
    Messages:
    205
    Likes Received:
    50
    Code:
    http://www.phpjabbers.com/redirect-based-on-referrer-or-ip-address-php2.html
    Code:
    <?
      $referrer = $_SERVER['HTTP_REFERER'];
      if (preg_match("/site1.com/",$referrer)) {
            header('Location:  http://www.customercare.com/page-site1.html');
      } elseif (preg_match("/site2.com/",$referrer)) {
            header('Location:  http://www.customercare.com/page-site2.html');
      };
      ?>