aaazalea
aaazalea

Reputation: 7930

Getting the URL of the redirecting page

I have a page to which users will be sent via a 301 redirect. Is there any way to get the URL of the specific page which redirected them through javascript?

EDIT: I can't use document.referrer because it is preserved or blank for a 301 redirect.

Upvotes: 0

Views: 830

Answers (2)

mplungjan
mplungjan

Reputation: 178412

Some suggestions

If you have access to the original page

  • and the target page is from the same domain, you can set a cookie
  • from the same server you can set a session variable and add it in a comment/hidden field on the 301
  • in either case you can pass the original URL in the query string

Upvotes: 2

Valentin Mercier
Valentin Mercier

Reputation: 5326

You will get it in with PHP

$_SERVER['HTTP_REFERER']

EDIT: The HTTP_REFERER parameter seems not to be working with 301 redirections, so what I would suggest is passing by GET parameters the previous url if you own the previous page, or using a PHP session if you want to hide the parameter.

header('Location: http://example.com/?ref='.urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"));

or

session_start();
$_SESSION['referer'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Then retrieve the data with either

$_GET["ref"];

or

session_start();
$_SESSION['referer'];

These are server sided solutions, but really easy to use.

You can add them to your javascript right after getting them :

<script>var referer = "<?php echo $_GET['ref']; ?>";</script>

EDIT2: Just get the GET parameter with javascript, so you will not need PHP

Upvotes: 0

Related Questions