redirect based on referer

bigfred

Power Member
Joined
Mar 15, 2009
Messages
747
Reaction score
146
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
 
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]
 
Hi great thanks. What is the [NC] and [R,L]?
 
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?
 
Basically I want anyone who clicks the link in the location UK, then it will redirect. Is this possible?
Do you want to redirect all UK traffic, no matter from which source it comes, or just UK traffic that comes from google?
 
I want all UK traffic from Google to be redirected
 
Try adding this line into .htaccess

RewriteCond %{http_referer} gl=uk [NC]
 
tried it, didnt work :(
 
that should do it i believe:

Code:
<?php

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

?>
 
thanks homenet for your help mate. Any other ideas?
 
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.
 
You are a superstar! It worked! Cheers mate appreciate all your time and help
 
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]

?
 
I'd say PHP is your best bet here, as you can change it page by page if need be. Just my $0.02
 
any idaes of how to do it via php?
 
that should do it i believe:

Code:
<?php

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

?>

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?
 
any idaes of how to do it via php?
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');
  };
  ?>
 
Back
Top