Creating 302 redirect to a URL that changes dynamically, and passing referring information?

1.1k views Asked by At

I want to redirect (302) my visitors of mydomain.com/page.php to another URL, that contains a dynamic value such as the visitor's IP address:

http://external.com/?ip=100.0.0.1

my common sense says this cannot be done with .htaccess, since the contents of it are static, and therefore the line would always be something like:

redirect 302 /redir-number-1 http://external.com/[not-useful-static-value]

Currently I'm first transferring the user to another PHP page (mydomain.com/transfer.php) with this redirection code:

$ip = $_SERVER['REMOTE_ADDR'] ;

$url = "http://external.com/?$ip;

header( "refresh:1;url=$url" );

But this doesn't seem to consist a 302 redirect, as the status code I get is 200, and it is also important for me to pass the original referring URL information (mydomain.com/page.php) when the visitor arrives to external.com, yet with my current method he arrives with mydomain.com/transfer.php as the referrer.

2

There are 2 answers

4
rjdown On

Try this for a 302:

header("Location: $url", true, 302);

But you are never going to get the correct referrer on a redirect. You need to add it as a variable in the URL maybe.

2
Croises On

You can do that with .htaccess in your root directory:

RewriteEngine on 
RewriteRule ^page\.php$ http://external.com/?ip=%{REMOTE_ADDR} [R=302,L]