网站如何制作301重定向

网址转向方法主要包括:301转向,302转向,JavaScript转向,PHP/ASP/CGI转向,META REFRESH网页META刷新,等。302转向可能会有URL规范化问题。其它方法都是常用的作弊手法,当然不是说不可以正当地用,方法本身没有错,但被作弊者用多了,搜索引擎对这些可疑的转向都很敏感。何必冒险呢。

当网页A用301重定向转到网页B时,搜索引擎可以肯定网页A永久的改变位置,或者说实际上不存在了,搜索引擎就会把网页B当作唯一有效目标。好处是,第一,没有网址规范化问题,第二,也很重要的,网页A的整个权重值会转移到网页B(目前百度及谷歌已声明:转移全部权重)。

Apache下301转向代码:

修改.htaccess文件,输入下列内容(需要开启mod_rewrite):

1)将不带WWW的域名转向到带WWW的域名下:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xunleipu.com [NC]

RewriteRule ^(.*)$ http://www.xxxx.com/$1 [L,R=301]

2)重定向到新域名:Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.xxxx.com/$1 [L,R=301]

Apache下vhosts.conf中配置301跳转:

Apache下vhosts.conf中配置301转向:<VirtualHost *:80>
ServerName www.xxx.com
DocumentRoot /home/
</VirtualHost><VirtualHost *:80>
ServerName xunleipu.com
RedirectMatch permanent ^/(.*) http://www.xxxx.com/$1
</VirtualHost>

Ruby容器中实现301跳转:

Ruby中实现301转向:def old_action
headers["Status"] = “301 Moved Permanently”
redirect_to “http://www.xunleipu.com”
end

Coldfusion容器中实现301跳转:

Coldfusion中实现301转向:<.cfheader statuscode=”301″ statustext=”Moved permanently”>
<.cfheader name=”Location” value=”http://www.xunleipu.com”>

如果你用的是 Windows主机,应该在控制面板做301转向设定。

据我所知,HTML无法做301转向。在HTML里只能做JS或META REFRESH,但不是301转向。而如果用JS跳转就属于302了,而不是正常返回码为:301

ASP的301跳转代码:

<%Response.Status="301 Moved Permanently"
Response.AddHeader "Location"," http://www.xx.com"
Response.End
%>

PHP的301跳转代码:

<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.xx.com");
?>

ASP.Net的301跳转代码:

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://www.xunleipu.com”);
}
</script>

CGI Perl的301跳转代码:

$q = new CGI;
print $q->redirect(”http://www.xunleipu.com”);

JSP的301跳转代码:

<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.xunleipu.com” );
response.setHeader( “Connection”, “close” );
%>

原文地址:https://www.cnblogs.com/jinnuohua/p/3748197.html