主流服务器环境下301永久定向代码集合

说到搜索引擎优化,涉及的方面和人员较多,不仅编辑需要组织内容,工程师在开发系统时也应该考虑到.我们的系统往往需要移植或者改变结构,这时候务必做好301转向,免得搜索引擎晕头.

下面是目前主流应用场景下的301转向设置.

1、JSP下的301转向代码

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

2、Apache下301转向代码

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

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

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^tianmomo.com [NC]
RewriteRule ^(.*)$ http://www.tianmomo.com/$1 [L,R=301]

2)重定向到新域名

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.tianmomo.com/$1 [L,R=301]

3)使用正则进行301转向,实现伪静态

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+).html$ news.php?id=$1

将news.php?id=123这样的地址转向到news-123.html
 

3、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.tianmomo.com/articles/301/“);
}
</script>

4、PHP下的301转向代码

header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.tianmomo.com/articles/301/”);
exit();

5、CGI Perl下的301转向代码

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

 

6、IIS下301设置

Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。

7、ASP下的301转向代码

<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.tianmomo.com/articles/301/”
%>

8、Apache下vhosts.conf中配置301转向

 

为实现URL规范化,SEO通常将不带WWW的域名转向到带WWW域名,vhosts.conf中配置为:

<VirtualHost *:80>
ServerName www.tianmomo.com
DocumentRoot /home/tianmomo
< irtualHost>

 

<VirtualHost *:80>
ServerName tianmomo.com
RedirectMatch permanent ^/(.*) http://www.tianmomo.com/$1
< irtualHost>
 

Apache下除了以上2种方法,还有其他配置方法和可选参数,建议阅读Apache文档。