1

Possible Duplicate:
How to identify if referrer is a 301 redirect

Using htaccess Im doing a 301 redirect to redirect old urls to the new urls, the issue is the url contains the id that is stored in the db. But I have a new db with new ids so now when the old url redirects to the new url a different record loads on the page. So what I did was store the old ids along with the new ids so I know what old id belongs to the new id. However how can I detect someone is trying to access the old url? Is there a way in php to detect the person coming to the page is being redirected via a 301? I dont want to add anything new to the url due to SEO reasons so I really need to know if I can detect if they came to this page via 301 redirect. Hope this makes sense.

Community
  • 1
  • 1
John
  • 9,840
  • 26
  • 91
  • 137
  • you can work with $_SERVER['HTTP_REFERER']; but this is not really reliable – Florian Kasper Dec 08 '12 at 19:50
  • 1
    @FlorianKasper: even if `HTTP_REFERER` is set it will point to the page where the link was clicked, not the script that sent 301. Such is the case with 301s. – Salman A Dec 08 '12 at 19:52
  • http://stackoverflow.com/questions/9496522/php-code-to-determine-if-a-user-was-301-redirected-to-my-site/9496657#9496657 – Salman A Dec 08 '12 at 19:54

4 Answers4

2

Add another parameter to the redirect like redirect=true, check the param to let your app know it was a redirect, start the session, and then redirect again to the final, unaltered url, compare the session to know, that a user came from the redirected url.

Or do what Salman says, use a canonical url.

markus
  • 40,136
  • 23
  • 97
  • 142
  • As mentioned in my original notes I cant due to SEO reasons. – John Dec 08 '12 at 19:55
  • That's why you redirect again! For the SEO reasons, the final url will have no additional param but your app will know. – markus Dec 08 '12 at 19:56
  • 4
    You can tell search engines about your canonical URL via ``. Use this to tell search engines that `?redirect=true` can be ignored. – Salman A Dec 08 '12 at 19:57
  • What's the point of the session? Besides that this is exactly the same concept as my answer. – Mike Dec 08 '12 at 20:01
  • The point of the session is to actually transfer the knowledge about the redirect into the app. Otherwise it's just another redirect and can come from whoever. – markus Dec 08 '12 at 20:33
  • @markus-tharkun I gotcha. I assumed that the point of letting the application know that it was a redirect was to pull the correct product from the database. – Mike Dec 08 '12 at 21:27
0

A script won't know that something has been redirected to it or not. Instead of redirecting to, say index.php?id=12345 add an intermediate redirect where you can determine the proper URL.

First, redirect in your .htaccess using a temporary variable name, e.g. index.php?oldid=12345 and then in that script do:

if (isset($_POST['oldid'])) {
    $id = fetch_new_id();
    header("Location: index.php?id=$id");
}
Mike
  • 23,542
  • 14
  • 76
  • 87
  • how does this affect SEO and passing the "juice" via 301 redirects twice? As I would need to do 2 301 redirects in your suggestion. – John Dec 08 '12 at 19:54
  • An extra 301 redirect isn't going to affect your SEO at all. – Mike Dec 08 '12 at 19:57
0

i think you cannot detect http status that was set, but using .htaccess you can add additional variable to redirected urls or rename id to another variable name.

0

Why not handle all the old URLs via php plus a rewrite and make the code directly 301 redirect to the new URL with the new id?

Tony McCreath
  • 2,882
  • 1
  • 14
  • 21