Jeyanth Kumar Jeyanth Kumar - 7 months ago 17
PHP Question

PHP redirected page doesn't have a referral url

I have two site

sitea.com
and
siteb.com


I need to redirect pages from
sitea.com
to
siteb.com
. The redirect script in
sitea.com
is

<?php
header("Location: http://siteb.com/somepage");
?>


The redirection is taking without any flaw. The functionality if fulfilled. But I have google analytics tracking on siteb.com which should show that the source is referral from sitea.com

I dug into web developer tools in google chrome and found that
siteb.com
doesn't send a referral header in the request.

How do i redirect my visitors from
sitea.com
to
siteb.com
which sends the referral headers on
siteb.com
.?

Answer

The best way to keep the referal is to issue a standard 301 from the server.

How to do this using .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

Doing it straight from PHP should also work (as pointed out by DaveRandom) just be sure to include the HTTP response code.

<?php
// 301 Moved Permanently
header("Location: http://siteb.com/somepage", TRUE, 301);

And bellow a table with data from diferent ways to re-direct and it's impact on analytics.

Redirect method             | Browser   | Result            | Impact on analytics
----------------------------+-----------+-------------------+--------------------
Meta refresh - 0            | Firefox 3 | Blank referrer    | Lost data
                            | IE8       | Blank referrer    | Lost data
                            | Opera 9   | Internal referrer | Lost data
Javascript:location.href    | Firefox 3 | Internal referrer | Lost data
                            | IE8       | Blank referrer    | Lost data
                            | Opera 9   | Internal referrer | Lost data
Javascript:location.replace | Firefox 3 | Internal referrer | Lost data
                            | IE8       | Blank referrer    | Lost data
                            | Opera 9   | Internal referrer | Lost data
Server-side 301             | Firefox 3 | Original referrer | 
                            | IE8       | Original referrer | 
                            | Opera 9   | Original referrer | 
Server-side 302             | Firefox 3 | Original referrer | 
                            | IE8       | Original referrer | 
                            | Opera 9   | Original referrer | 
Server-side 301 - chained   | Firefox 3 | Original referrer | 
                            | IE8       | Original referrer | 
                            | Opera 9   | Original referrer | 
Server-side 302 - chained   | Firefox 3 | Original referrer | 
                            | IE8       | Original referrer | 
                            | Opera 9   | Original referrer | 

Original link for the above table.

Comments