HTTP返回码中301与302的区别
一.官方说法
301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于:
301 redirect: 301 代表永久性转移(Permanently Moved)。
302 redirect: 302 代表暂时性转移(Temporarily Moved )。
这是很官方的说法,那么它们的区别到底是什么呢?
二.现实中的差异
2.1.对于用户
301,302对用户来说没有区别,他们看到效果只是一个跳转,浏览器中旧的URL变成了新的URL。页面跳到了这个新的url指向的地方。
2.2.对于引擎及站长
2.2.1 302
302转向可能会有URL规范化及网址劫持的问题。可能被搜索引擎判为可疑转向,甚至认为是作弊。
网址规范化
请参见:http://www.chinamyhosting.com/seoblog/2006/04/10/url-canonicalization/
网址劫持
302重定向和网址劫持(URL hijacking)有什么关系呢?这要从搜索引擎如何处理302转向说起。从定义来说,从网址A做一个302重定向到网址B时,主机服务器的隐含意思是网址A随时有可能改主意,重新显示本身的内容或转向其他的地方。大部分的搜索引擎在大部分情况下,当收到302重定向时,一般只要去抓取目标网址就可以了,也就是说网址B。
实际上如果搜索引擎在遇到302转向时,百分之百的都抓取目标网址B的话,就不用担心网址URL劫持了。
问题就在于,有的时候搜索引擎,尤其是Google,并不能总是抓取目标网址。为什么呢?比如说,有的时候A网址很短,但是它做了一个302重定向到B网址,而B网址是一个很长的乱七八糟的URL网址,甚至还有可能包含一些问号之类的参数。很自然的,A网址更加用户友好,而B网址既难看,又不用户友好。这时Google很有可能会仍然显示网址A。
由于搜索引擎排名算法只是程序而不是人,在遇到302重定向的时候,并不能像人一样的去准确判定哪一个网址更适当,这就造成了网址URL劫持的可能性。也就是说,一个不道德的人在他自己的网址A做一个302重定向到你的网址B,出于某种原因, Google搜索结果所显示的仍然是网址A,但是所用的网页内容却是你的网址B上的内容,这种情况就叫做网址URL劫持。你辛辛苦苦所写的内容就这样被别人偷走了
2.2.2 301
当网页A用301重定向转到网页B时,搜索引擎可以肯定网页A永久的改变位置,或者说实际上不存在了,搜索引擎就会把网页B当作唯一有效目标。
301的好处是:
第一, 没有网址规范化问题。
第二, 也很重要的,网页A的PR网页级别会传到网页B。
三.Apache中实现301、302
方法一,url rewrite,mod_rewrite
Rewriteengine on
RewriteCond %{HTTP_HOST} ^cmp.soso.com [NC]
RewriteRule ^/js/(.*) http://www.soso.com/js/$1 [R=301]
NC(no case) 不区分大小写
ServerName cmp.soso.com
将cmp.soso.com中js目录的下所有访问重定向到http://www.soso.com/js/,指定跳转返回码为301。
对于[R=301]的详解:
‘redirect|R [=code]‘ (强制重定向 redirect)
以http://thishost[:thisport]/(使新的URL成为一个URI) 为前缀的Substitution可以强制性执行一个外部重定向。如果code没有指定,则产生一个HTTP响应代码302(临时性移动)。 如果需要使用在300-400范围内的其他响应代码,只需在此指定这个数值即可,另外,还可以使用下列符号名称之一: temp (默认的), permanent, seeother. 用它可以把规范化的URL反馈给客户端,如, 重写“/~”为 “/u/”,或对/u/user加上斜杠,等等。
注意: 在使用这个标记时,必须确保该替换字段是一个有效的URL! 否则,它会指向一个无效的位置! 并且要记住,此标记本身只是对URL加上 http://thishost[:thisport]/的前缀,重写操作仍然会继续。 通常,你会希望停止重写操作而立即重定向,则还需要使用’L'标记.
方法二 Redirect ,涉及模块:mod_alias
例:
<VirtualHost 10.1.146.163:80>
DocumentRoot /home/qmhball/web/mybranches/stat_3276/oa/
ServerName oalogin.com
Redirect 301 /login.php http://www.soso.com
</VirtualHost>
将oalogin.com下对login.php的访问重定向到http://www.soso.com,返回码301。
如果没有指定redirect的返回值(上例中的301),则默认重定向是”临时性的”(HTTP status 302)。
4 302 Object Moved
用过ASP的人都知道ASP中页面重定向至少有Redirect和Transfer两种方法。二的区别在于Redirect是客户端重定向,而Transfer是服务器端重定向,那么它们具体是如何通过HTTP消息头实现的呢?
先来看一下Transfer的例子:
例如ASP文件1.asp只有一行
<% Server.Transfer “1.htm” %>
HTML文件1.htm也只有一行:
<p>this is 1.htm</p>
如果我们从浏览器里请求1.asp,发送的请求是:
GET /1.asp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: localhost:8080
Connection: Keep-Alive
Cookie: ASPSESSIONIDACCTRTTT=PKKDJOPBAKMAMBNANIPIFDAP
注意请求的文件确实是1.asp,而得到的回应则是:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Mon, 06 Mar 2006 12:52:44 GMT
X-Powered-By: ASP.NET
Content-Length: 20
Content-Type: text/html
Cache-control: private
<p>this is 1.htm</p>
不难看出,通过Server.Transfer语句服务器端已经做了页面重定向,而客户端对此一无所知,表面上看上去得到的就是1.asp的结果。
如果把1.asp的内容改为:
<% Response.Redirect “1.htm” %>
再次请求1.asp,发送的请求没有变化,得到的回应却变成了:
HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.1
Date: Mon, 06 Mar 2006 12:55:57 GMT
X-Powered-By: ASP.NET
Location: 1.htm
Content-Length: 121
Content-Type: text/html
Cache-control: private
<head><title>Object moved</title></head>
<body><h1>Object Moved</h1>This object may be found <a HREF=”">here</a>.</body>
注意HTTP的返回代码由200变成了302,表示这是一个重定向消息,客户端需要根据消息头中Location字段的值重新发送请求,于是就有了下面一组对话:
GET /1.htm HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: localhost:8080
Connection: Keep-Alive
If-Modified-Since: Thu, 02 Mar 2006 06:50:13 GMT
If-None-Match: “b224758ec53dc61:9f0″
Cookie: ASPSESSIONIDACCTRTTT=PKKDJOPBAKMAMBNANIPIFDAP
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Date: Mon, 06 Mar 2006 12:55:57 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Mon, 06 Mar 2006 12:52:32 GMT
ETag: “76d85bd51c41c61:9f0″
Content-Length: 20
<p>this is 1.htm</p>
很明显,两种重定向方式虽然看上去结果很像,但在实现原理上有很大的不同。
為 Kohana 添加 Smarty
1、到http://www.smarty.net/下載最新的smarty,解壓把lib放到system\vendor下面。然後把lib改名成smarty。
2、在modules创建config,controller等文件夹。
3、修改application\config\config.php
$config['modules'] = array
(
// MODPATH.'auth', // Authentication
// MODPATH.'kodoc', // Self-generating documentation
// MODPATH.'gmaps', // Google Maps integration
// MODPATH.'archive', // Archive utility
// MODPATH.'payment', // Online payments
// MODPATH.'unit_test', // Unit testing
MODPATH . 'smarty'
);
4,kohana加入smarty遇到的问题。
- 模板不解析变量。
有两种方法。
一,在kohana/modules/smarty/libraries/下面添加MY_controller.php
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Controller extends Controller_Core {
function __construct()
{
parent::__construct();
if (Kohana::config('smarty.integration') == TRUE)
{
$this->MY_Smarty = new MY_Smarty;
}
}
public function _kohana_load_view($template, $vars)
{
if ($template == '')
return;
if (substr(strrchr($template, '.'), 1) === Kohana::config('smarty.templates_ext'))
{
// Assign variables to the template
if (is_array($vars) AND count($vars) > 0)
{
foreach ($vars AS $key => $val)
{
$this->MY_Smarty->assign($key, $val);
//echo "assign key=$key,value=$val";
}
}
// Send Kohana::instance to all templates
$this->MY_Smarty->assign('this', $this);
// Fetch the output
$output = $this->MY_Smarty->fetch($template);
}
else
{
$output = parent::_kohana_load_view($template, $vars);
}
return $output;
}
}
注:这种方法对于kohana2.4版本,而且controller_core类要有 _kohana_load_view方法,view_core的render()方法有下一行代码
$output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data);
不满足上面条件的可以试试下一种方法。
二,
条件:1,kohana的view_core类有load_view()方法,
2,view_core类的render()方法有如下行
$output = $this->load_view($this->kohana_filename, $data);
方法:添加kohana/modules/smarty/libraries/MY_View.php
<?php defined('SYSPATH') OR die('No direct access allowed.');
class View extends View_Core {
public function __construct($name, $data = NULL, $type = NULL)
{
$smarty_ext = Kohana::config('smarty.templates_ext');
if (Kohana::config('smarty.integration') == TRUE AND Kohana::find_file('views', $name, FALSE, (empty($type) ? $smarty_ext : $type)))
{
$type = empty($type) ? $smarty_ext : $type;
}
parent::__construct($name, $data, $type);
$this->kohana_filetype = $type;
if (Kohana::config('smarty.integration') == TRUE)
{
$this->MY_Smarty = new MY_Smarty;
}
}
public function load_view($template, $vars)
{
if ($template == '')
return;
if (substr(strrchr($template, '.'), 1) === Kohana::config('smarty.templates_ext'))
{
// Assign variables to the template
if (is_array($vars) AND count($vars) > 0)
{
foreach ($vars AS $key => $val)
{
$this->MY_Smarty->assign($key, $val);
//echo "assign key=$key,value=$val";
}
}
// Send Kohana::instance to all templates
$this->MY_Smarty->assign('this', $this);
// Fetch the output
$output = $this->MY_Smarty->fetch($template);
}
else
{
$output = parent::load_view($template, $vars);
}
return $output;
}
}
就可以搞定了。