1 PHP 301 重定向代码
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.mvpec.com/articles/301/”);
exit();
 
2 Apache301 重定向代码
新建 .htaccess文件,输入下列内容(需要开启 mod_rewrite):
1)将不带 WWW的域名转向到带 WWW的域名下
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mvpec.com [NC]
RewriteRule ^(.*)$ http://www.mvpec.com/$1 [L,R=301]
2)重定向到新域名
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.mvpec.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 301重定向代码
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.mvpec.com/articles/301/”
%>
4 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.mvpec.com/articles/301/“);
}
</script>
5 Apachevhosts.conf中配置301转向
为实现 URL规范化, SEO通常将不带 WWW的域名转向到带 WWW域名, vhosts.conf中配置为:
<VirtualHost *:80>
ServerName www.mvpec.com
DocumentRoot /home/mvpec
</VirtualHost>
<VirtualHost *:80>
ServerName lesishu.cn
RedirectMatch permanent ^/(.*) http://www.mvpec.com/$1
</VirtualHost>
 
6 JSP 301重定向代码
<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.mvpec.com/” );
response.setHeader( “Connection”, “close” );
%>
7 CGI Perl 301重定向代码
$q = new CGI;
print $q->redirect(”http://www.new-url.com/”);

8 nginx 301重定向代码
www.hxj.comhxj.com合并,并把之前的域名也一并合并 . 有两种实现方法 ,第一种方法是判断 nginx核心变量 host(老版本是 http_host)
server {
server_name www.hxj.com hxj.com ;
if ($host != 'www.hxj.com' ) {
rewrite ^/(.*)$ http://www.hxj.com/$1 permanent;
}
...
}
第二种方法:
server {
server_name hxj.com;
rewrite ^/(.*) http://www.hxj.com/$1 permanent;
}
这两种方法中, permanent是关键,详细说明见 nginx重定向规则说明
last – 基本上都用这个 Flag
break –
中止 Rewirte,不在继续匹配
redirect –
返回临时重定向的 HTTP状态 302
permanent –
返回永久重定向的 HTTP状态 301

 
301 转向情况检测
  1. http://www.seoconsultants.com/tools/headers.asp
  2. http://www.internetofficer.com/seo-tool/redirect-check/