上一篇:Wordpress性能优化 - 下一篇:Wordpress数据库分析: 整体结构
版权声明:可以任意转载,但转载时必须标明原作者charlee、原始链接http://tech.idv2.com/2007/01/02/permanent-redirect-with-http-301/以及本声明。
301就是HTTP协议中的应答状态301 Moved Permanently。 当你的网站更换地址后,应当在原地址上写个程序返回301, 以便让访问者能够知道你更换了地址,也让搜索引擎能迅速地收录新地址。 我的blog更改地址之后,经fcicq的提醒, 使用 mod_rewrite 返回301,三天后就有100多页面被Google收录。
下面是使用各种语言返回301的方法。调试时可使用Firefox+ Tamper Data插件, 也可以用IE的ieHTTPHeaders插件。
ASP
直接调用Response.Redirect函数会返回302 Object Moved。为了返回301, 我们需要手工设置信息。
<%@ Language=VBScript %> <% Response.Status = "301 Moved Permanently" Response.AddHeader "Location", "http://www.inspiremedia.org/" Response.End %>
PHP
简单地调用 header() 函数输出头信息即可。
<?php
header("301 Moved Permanently");
header("Location: http://www.inspiremedia.org/");
?>
Perl
#!/usr/bin/perl -w print "Status: 301 Moved Permanently\n"; print "Location: http://www.inspiremedia.org/\n\n";
mod_rewrite
如果你使用Apache,那么用mod_rewrite进行重定向是最简单的选择了, 只要在 httpd.conf 的全局配置或者虚拟主机配置中写一条规则即可。
RewriteEngine On RewriteRule ^/(.*) http://www.inspiremedia.org/$1 [R=permanent,L]
注意一定要使用 [R=permanent] 选项,仅用 [R] 选项会返回 302 。
2007-01-03 02:18
呵呵,确实比较奇怪。一般的应用中 header(”Location: url”) 等还应该是302才对。
2008-04-02 20:50
哇
不错的经验耶

2007-01-02 23:40
多嘴一句.
lighttpd中rewrite暂时只支持301,呵呵.
偶奇怪这个…暂时还是没法选的,想要302的居然要自己改代码.