优质网页小贴士 - 使用标准的重定向:不要破坏后退按钮!
网址:http://doc.ajaxeye.com/blog/110/
应该使用的方法和应该避免的技术
不要使用“refresh”进行重定向
如果你想让地址 http://www.example.org/foo 显示存放在 http://www.example.org/bar 的页面内容,你不应该像这样使用“refresh”技术:
<META HTTP-EQUIV=REFRESH CONTENT="1; URL=http://www.example.org/bar">
为什么呢?因为它将破坏“后退”按钮。想象一下读者按下“后退”按钮之后,网页将被再次刷新,读者将看到同一个页面。读者很有可能会变得不舒服,然后关闭浏览器窗口——这是作为网页作者的你不太想得到的效果吧。
请使用 HTTP 重定向
在使用“refresh” meta 标签的处理重定向的时候,我们用的是一个文档内部的特殊指令。用户代理(浏览器或是 Markup Validator)会下载该页面,查找其上下文,找到“refresh”指令,等待指定的时间(设置“0”秒,可以得到立即刷新的效果,也可以设置具体的等待秒数),然后继续导入新地址。
另一方面,“HTTP 重定向”执行得更直接,因为它是在 另一层内 完成的。当 User Agent (i.e. a browser or the validator) 首次访问服务器并请求该文档时,服务器自身就会判断出该文档要重定向到另一个地址,于是服务器便主动告知 user-agent 它应该直接访问新地址。
同时,“HTTP 重定向”能提供更多的方法进行重定向,因为它除了能提供给 User Agent 新地址外,还有很多功能:服务器能提供关于重定向的目的和类型的信息,这些信息允许 User Agent 根据不同的重定向种类进行不同的动作。HTTP 重定向(根据服务器发送的相关 HTTP status code)的种类如下:Permanent Redirect (HTTP 301)、 Temporary Redirect (307)、undefined redirect (302)。
参阅下面的文档和教程学习如何通过你的服务器使用它。
延伸阅读
- The Top Ten New Mistakes of Web Design, by Jakob Nielsen, offers a view of why refresh should not be used for redirecting a document to a new address, as well as other (do's and) don't's on web design.
- The section 10.3 Redirection 3xx in Hypertext Transfer Protocol -- HTTP/1.1, is the authoritative documentation on HTTP redirects.
- The "Redirect" directive in Apache's manual
- The Apache URL rewriting guide
- Appendix B: API Reference in the Zope Book
- redirection in Microsoft IIS documentation
- using the
headerfunction in PHP
本文相关评论|Comments