PHP code to determine if a user was 301 redirected to my site

别来无恙 提交于 2019-12-24 00:24:55

问题


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 :-)


回答1:


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


来源:https://stackoverflow.com/questions/9496522/php-code-to-determine-if-a-user-was-301-redirected-to-my-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!