如何做301转向


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

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

先说在web服务软件

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

  1. 选中“重定向到URL”
  2. 在对话框中输入目标页面的地址
  3. 选中“资源的永久重定向”
  4. 点击“应用

如果是apache方法多一些,也方便一些
如果要把访问www.juhui.com的请求全部301到www.juyimeng.com

<VirtualHost www.juhui.com>
Redirect 301 / http://www.juyimeng.com/
CustomLog logs/redir "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
</VirtualHost>

上面的
Redirect 301 / http://www.juyimeng.com/
可以写为
Redirect permanent / http://www.juyimeng.com/

也可以用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.test.com" ); 
?> 

用JSP (Java)来做301重定向

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

用CGI PERL 来做301重定向

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

用ASP来做301重定向

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

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

用Ruby on Rails Redirect来做301重定向

def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “http://www.test.com/”
end

html的meta refresh和javascript也可以转向

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

<script>
window.location="http://www.test.com/test.html";
</script>

但是上述的方法不推荐

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

上述改造是否成功,可以用httpwatch来查看


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据