平时做SEO遇到301转向的时候,总找不到相关的信息,这里软载“点石”一篇文章,到用到时候可以很快找到。
1、IIS下301设置
Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。
2、ASP下的301转向代码
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.jarfee.cn/articles/301/”
%>
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.jarfee.cn/articles/301/“);
}
</script>
4、PHP下的301转向代码
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.jarfee.cn/articles/301/”);
exit();
5、CGI Perl下的301转向代码
$q = new CGI;
print $q->redirect(”http://www.jarfee.cn/”);
6、JSP下的301转向代码
<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.jarfee.cn/” );
response.setHeader( “Connection”, “close” );
%>
7、Apache下301转向代码
新建.htaccess文件,输入下列内容(需要开启mod_rewrite):
1)将不带WWW的域名转向到带WWW的域名下
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jarfee.cn [NC]
RewriteRule ^(.*)$ http://www.jarfee.cn/$1 [L,R=301]
2)重定向到新域名
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.jarfee.cn/$1 [L,R=301]
3)使用正则进行301转向,实现伪静态
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+)\.html$ news.php?id=$1
将news.php?id=123这样的地址转向到news-123.html
8、Apache下vhosts.conf中配置301转向
为实现URL规范化,SEO通常将不带WWW的域名转向到带WWW域名,vhosts.conf中配置为:
<VirtualHost *:80>
ServerName www.jarfee.cn
DocumentRoot /home/jarfee
</VirtualHost>
<VirtualHost *:80>
ServerName jarfee.cn
RedirectMatch permanent ^/(.*) http://www.jarfee.cn/$1
</VirtualHost>
Apache下除了以上2种方法,还有其他配置方法和可选参数,建议阅读Apache文档。
301转向情况检测
1. http://www.seoconsultants.com/tools/headers.asp
2. http://www.internetofficer.com/seo-tool/redirect-check/
(注:如果有朋友要用上面代码,请将jarfee.cn换成你自己的域名。原贴:http://www.dunsh.org/2008/03/19/301-redirect-code/)
![Jarfee[加飞]的博客](http://www.jarfee.cn/blog/wp-content/themes/atahualpa/images/logo.png)
[...] 最近网站该版,有很多之前的URL要重新转向到新版本的URL上,而且要一一对应。大家都知道,网站改版时的URL转向一般要用301 Moved Permanently,如果是英文或者数字格式的URL,好办,直接把规则写进.htaccess里面就行了,但是如果URL里含有汉字,全角表点符号怎么办?直接写进去是不能正常工作的。举个例子: Redirect 301 /(jarfee).html http://localhost/jarfee-sbg-case.html [...]
[...] 之前有一个301跳转汇总的贴子,写和一些有关301跳转的各种方法,但是有时候我们会有特殊要求,比如:我现在的域名需要将不带www的跳转到带www的域名中,网站又不支持.htaccess,怎么办? [...]
[...] 于是根据301 跳转规则,写了下面的代码,追加到 .htaccess里面: RewriteCond %{HTTP_HOST} ^jarfee.cn [NC] RewriteRule ^(.*)$ http://www.jarfee.cn/$1 [L,R=301] 测试后发现,跳转是跳转了,却跳到了动态的URL上。比如: http://jarfee.cn/goods-33.html 跳转到了 http://www.jarfee.cn/goods.php?id=33 本来应该跳转到:http://www.jarfee.cn/goods-33.html 。。。 [...]