如何做301重定向(301转向)

May 19, 2008 | tags 301转向  搜索引擎   | views
Comments 4

301 redirect: 301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好(SEO)的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址。

现在知道的有2类,一类是在web服务软件上做,还有就是在程序里面做

先说在web服务软件

如果是iis:
打开internet信息服务管理器,在要重定向的网页或目录上按右键

选中“重定向到URL”
在对话框中输入目标页面的地址
选中“资源的永久重定向”
点击“应用
如果是apache方法多一些,也方便一些
如果要把访问www.yinfengming.org的请求全部301到www.juyimeng.com

<VirtualHost www.yinfengming.org>
Redirect 301 / http://www.yinfengming.org/
CustomLog logs/redir "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
</VirtualHost>
上面的
Redirect 301 / http://www.yinfengming.org/
可以写为
Redirect permanent / http://www.yinfengming.org/

也可以用apache的.htaccess,怎么使.htaccess起效,点这里
.htaccess文件的内容

RewriteEngine on
rewriterule abc\.html /index\.html [R=301]
其总用是把abc.html301到index.html

用程序来做

用PHP来做301重定向

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.yinfengming.org" );
?>
用JSP (Java)来做301重定向

<%
response.setStatus(301);
response.setHeader( "Location", "http://www.yinfengming.org/" );
response.setHeader( "Connection", "close" );
%>
用CGI PERL 来做301重定向

$q = new CGI;
print $q->redirect("http:/www.yinfengming.org/");
用ASP来做301重定向

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently";
Response.AddHeader("Location","http://www.yinfengming.org/");
%>
用ColdFusion 来做301重定向

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.yinfengming.org">
用ASP .NET Redirect来做301重定向

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.yinfengming.org");
}
</script>
用Ruby on Rails Redirect来做301重定向

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.yinfengming.org/"
end
html的meta refresh和javascript也可以转向

<meta http-equiv="Refresh" content="0; url=http://yinfengming.org/page.html">

<script>
window.location=http://www.yinfengming.org;
</script>
但是上述的方法不推荐

如果要Redirect的源url是html这种结尾也可以,改apache的配置,把html扩展名的文件当Php或者其他脚本来处理就可以了
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
 




发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。