Reputation: 7930
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
Reputation: 178412
Some suggestions
If you have access to the original page
Upvotes: 2
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