views:

117

answers:

3

Hi all,

I'm doing a 301 redirect on site A to site B - when the user arrives at site B it needs to find the page the user came from. This doesn't seem to be working though:

$_SERVER['HTTP_REFERER']

whereas if I do a link to the page

<a href="http://site-b.com"&gt;go&lt;/a&gt;

I get the referrer through. Is there a reason it doesn't come through after the redirect? If so can anyone offer any advice on how to do this. I want to avoid at all costs having a query string on the redirect.

Is there maybe another header I need to send with the page that redirects?

Thanks for any advice!

A: 

You could try adding a CGI query string to the end of your URL when doing the redirect -- eg

http://www.site-b.com?redirectfrom=www.site-a.com

site-b.com would ignore the URL parameter, but it would be recorded in the logs and would be accessible from within PHP.

Spudley
The original poster says that he wants to avoid query strings at all costs so I don't think this will be what they're after
Nev Stokes
yes, query string isn't really an option, thanks for the suggestion though.
kron
A: 

The thing is, the HTTP_REFERER is site A. That's just how a 301 works.

That said, the easy way to do this is to take the url of the referrer to site A onto the end of site B's URL as a variable. Then, at site B, any time you have a referral from site A, you can have it.

<?php
    header("Location: http://site-b.com/?ref="
            .urlencode($_SERVER['HTTP_REFERER']),TRUE,301);
?>

Then of course at site B, access urldecode($_GET['ref']) for your referrer.

However... if you're looking to avoid _GET variables, you have a few options.

A) Collect the _GET request, then re-munge the URL -- IE have site B redirect to a "clean" version of itself.

B) Have your redirecting page make a curl or stream_get_contents over to a "collection" page prior to issuing a header(), where you collect and store the any session information (like the refererer) and have it prepared to be processesed when they redirect.

sleepynate
A: 

You can do it with javascript. Use the following script, but it between <head> and </head>

<script type="text/javascript">
location.href='http://www.site-b.com';
</script>

This will of course not make a proper HTTP 301 redir, but I just tested it, and it will send the referer (the referer being site-a).

Birk