2

Is it possible in PHP to know if a user has been 301 redirected to my website?

So in myoldsite.com I have a 301 redirect in the .htaccess file to mynewsite.com.

Can I place some PHP code in mynewsite.com to recognise when a user has been 301 redirected? I just need this to display a specific message if this is the case.

Many thanks for any pointers here :-)

Salman A
  • 262,204
  • 82
  • 430
  • 521
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

1 Answers1

2

I believe with 301-redirects the HTTP_REFERER field will contain the original referer:

  • User enters oldsite.com in address bar and 301 redirected to newsite.com
    • referer = ""
  • User is visiting 3rdparty.com, clicks a link pointing to oldsite.com and 301 redirected to newsite.com
    • referer = "3rdparty.com"

This behavior somewhat makes sense for 301 redirects.

Solution: I think you should add a query-string parameter to your 301 response such as ref=oldsite and check its value on your new website.

If you are concerned about having query string parameters in your URLs, you can tell search engines to ignore specific query string parameters. The procedure varies with search engines. Apparently there is a trick that works across all major search engines: the <link rel="canonical"> tag. Examples:

  • http://newsite.com/?ref=oldsite
    • http://newsite.com/
  • http://newsite.com/?ref=oldsite&page=main and
  • http://newsite.com/?page=main&ref=oldsite
    • http://newsite.com/?page=main
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Thanks for the reply, Salman :-) So my .htaccess could read: `RewriteEngine On Redirect 301 /index.php?ref=oldsite http://www.newsite.com/` And on 'newsite.com' have a script that checks for ref equals oldsite? – michaelmcgurk Feb 29 '12 at 09:46
  • Yes the idea is right but not sure about the syntax of `Rewrite`, perhaps it should be `Redirect 301 /index.php http://newsite.com/?ref=oldsite` – Salman A Feb 29 '12 at 09:54
  • That was supposed to be "Now to tell...". Out of intersest, how do I achieve this in Webmaster Tools? – michaelmcgurk Feb 29 '12 at 10:15
  • 2
    A cross-search-engine way to tell search engines that they should ignore the `ref=whatever` parameter is to use the [link rel="canonical"](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394) tag. The canonical URL of the main page of newsite.com would be *newsite.com plus (query string minus ref=whatever if there was a query string)* – Salman A Feb 29 '12 at 10:50
  • Perfect. I'm now using (pseudo code) `if ($script == index.php) then end if`. Is this good to go? – michaelmcgurk Feb 29 '12 at 11:11
  • Can I add "ref" to the Google Webmaster Tools "URL Parameters" field too? – michaelmcgurk Feb 29 '12 at 11:17
  • Aaaah cool :-) Many thanks for all your help and patience with this. Very much appreciate it and have learned lots. Thanks. – michaelmcgurk Feb 29 '12 at 11:21
  • Answer accepted :-) Will quality control some comments shortly. – michaelmcgurk Feb 29 '12 at 11:41