网站返回503

网站需要临时维护,这是个经常性的问题,在这期间必须关闭网站一小段时间,而在这段维护的期间内,我们应该做些什么,我们应该向搜索引擎返回什么样的header信息?200?404?500?甚至停止解析?
NO!Google告诉我们,这个时候网站应该返回:“503 Service Temporarily Unavailable”!
如果你的网站正在维护,而此时 Google来抓取页面,发现你的页面返回了 404 或 500错误,一次两次可能还没影响,但是3次4次后,你的网站权重或PR值将会受到明显的影响甚至清除。
什么是 503 Service Temporarily Unavailable Header?
那么,我们如何正确的使用 503 Service Temporarily Unavailable 这个header状态码呢?下面举的例子需要配合APACHE的 .htaccess 重写文件来实现:
仅仅向google的蜘蛛发送HTTP 503:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} ^.*(Googlebot|Googlebot|Mediapartners|Adsbot|Feedfetcher)-?(Google|Image)? [NC]
# or RewriteCond %{HTTP_USER_AGENT} ^.*google.* [NC]
RewriteRule .* /cgi-bin/error/503.php

 
向除了指定ip外的任何来访者发送 503: 

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !^/cgi-bin/error/503.php [NC]
RewriteRule .* /cgi-bin/error/503.php

 

向蜘蛛发送 503,其他来访者返回一个 404 页面:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^.*(Googlebot|Googlebot|Mediapartners|Adsbot|Feedfetcher)-?(Google|Image)? [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/error/503.php [NC]
RewriteRule .* /cgi-bin/error/503.php

RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !^/cgi-bin/error/404.php [NC]
RewriteRule .* /under-development-explain.html [R=302,L]

 

PHP代码中返回 503::

<?php
ob_start();
header(‘HTTP/1.1 503 Service Temporarily Unavailable’);
header(‘Status: 503 Service Temporarily Unavailable’);
header(‘Retry-After: 3600′);
header(‘X-Powered-By:’);
?><!DOCTYPE HTML PUBLIC ”-//IETF//DTD HTML 2.0//EN”>
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>

 
上面 .htaccess 里的 503.php 页面就可以直接使用上面的这段 php代码。

手机扫一扫,欢迎关注公众号

关注程序员成长

成长的乐趣,在于分享!
大龄程序员,一路走来,感慨颇多。闲暇时写写字,希望能给同行人一点帮助。
本文版权归作者growithus和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/growithus/p/11012266.html