如何识别引用者是否是 301 重定向 [英] How to identify if referrer is a 301 redirect

查看:24
本文介绍了如何识别引用者是否是 301 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的网站实施一个 slug 系统.我计划将无效的 slug 重定向到存储在数据库中的正确位置.例如

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

点击 db,检查 11 的 slug 是不是错误的 slug 如果不是就做 301 重定向

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

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

检测 301 并通知用户他们访问了无效链接

Detect 301 and inform user that they followed an invalid link

是否可以最好使用 PHP 识别 301 重定向,以便我可以要求用户更新那里的书签等.

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

谢谢,杰米.

推荐答案

想到了几个解决方案:

  1. 你可以试试 get_headers() 啊不,它向 URL 发送请求,这不是你想要的

因为无论如何您都是从同一台机器重定向和测试重定向,所以您可以简单地将有关错误 slug 的消息写入用户会话,并在下次呈现视图模板时显示它.许多框架都有一个 flash messenger 组件,可以让您轻松地做到这一点,例如使用 Zend Framework,您将在控制器操作中使用以下代码

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('我们在上次请求中做了一些事情');

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

当发现错误的 slug 时,不是立即重定向,而是渲染一个 View 模板,告诉用户错误的 slug,然后使用模板中的元或 javascript 重定向.或者,您还可以使用正确的 slug 编写一个指向模板的普通链接.

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.

最后,您还可以通过 header(),然后可以从您的脚本中读取. 也不起作用,因为一旦浏览器从服务器获得重定向,它们就会丢失

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

这篇关于如何识别引用者是否是 301 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆