duan19850312 2009-12-26 13:06
浏览 66
已采纳

如何识别引用者是否为301重定向

I'm implementing a slug system for my website at the moment. I plan to redirect invalid slugs to the correct on that is stored in the database. E.g.

http://example.com/11/wrong-slug

Hit db, check if 11's slug is wrong-slug if not do 301 redirect

http://example.com/11/right-slug

Detect 301 and inform user that they followed an invalid link

Is it possible to identify the 301 redirect preferably using PHP so I can ask the user to update there bookmark etc.

Thanks, Jamie.

  • 写回答

5条回答 默认 最新

  • dongyi6183 2009-12-26 13:11
    关注

    Several solutions come to mind:

    1. You could try with get_headers() ah no, it dispatches a request to the URL, which is not what you want

    2. Since you are redirecting and testing for the redirect from the same machine anyway, you could simply write a message about the wrong slug into the user's session and display it the next time the view template is rendered. Many frameworks have a flash messenger component, that allow you do that easily, e.g. with Zend Framework you would use the following code in your controller action

      $flashMessenger = $this->_helper->getHelper('FlashMessenger'); $flashMessenger->addMessage('We did something in the last request');

    3. Instead of redirecting immediately when a wrong slug is found, you just render a View template that tells your user about the wrong slug and then use a meta or javascript redirect from the template. Optionally, you'd also write a plain link with the right slug to the template.

    4. And finally, you could also inject a custom header into the redirect via header(), which can then be read from your script. won't work either, because they will be lost once the browser got the redirect from the server

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计