如果一个网站拥有两个域名:domain1.com和domain2.com。
在网站运营前期,主推domain1.com,但发展到中期,由于品牌或产品定位的原因,domain1.com这个域名不再适合当前情况,要更换成domain2.com。
作为SEO,我们肯定推荐使用301跳转,当用户访问domain1.com时,自动引导用户进入新域名domain2.com,可以通过程序或域名跳转实现301跳转,这样可以将旧域名PR传递到新域名。
但是如果用户收藏了旧域名的某个内容页(譬如:domain1.com/content/20070808.html),通过域名跳转直接引导到domain2.com首页恐怕会产生较差用户体验,特别是对未及时获知域名更换的用户;最好方式是实现通配,动态的将旧域名某个内页跳转到新域名相同页面的URL,那如何用技术手段实现新旧内页URL之间的301跳转???
即:从domain1.com/content/20070808.html跳转到domain2.com/content/20070808.html,并使浏览器返回301代码
例如:
大旗网域名由chinabbs.com换成daqi.com
博客网域名由blogchina.com换成bokee.com
有兴趣或者有解决办法的程序员,请留下你的联系方式或者发表评论,一起探讨下关于URL重定向的相关问题。
感谢dawnh的提示,我有点恍然大悟,PHP下的实现方式(参考):
.htaccess文件代码如下(bloghuman.com的.htaccess如此设置):
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^bloghuman.com [nc]
rewriterule ^(.*)$ http://www.bloghuman.com/$1 [r=301,nc]
RewriteEngine on
rewritecond %{http_host} ^bloghuman.com [nc]
rewriterule ^(.*)$ http://www.bloghuman.com/$1 [r=301,nc]
注释1:如果用户访问http://bloghuman.com/,则跳转至http://www.bloghuman.com,且返回301状态码;当用户访问http://bloghuman.com/post/301.htm,则跳转至http://www.bloghuman.com/post/301.htm,并返回301状态码。
注释2:测试URL:http://www.wlxs.com.cn/,你可以访问http://www.wlxs.com.cn/post/301.htm
我在wlxs.com.cn下配置了.htaccess文件,使其301跳转至http://www.bloghuman.com/post/301.htm
我的.htaccess配置实现了由主域名(bloghuman.com)301跳转至二级域名(www.bloghuman.com);
结论:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.domain1.com [nc]
rewriterule ^(.*)$ http://www.domain2.com/$1 [r=301,nc]
RewriteEngine on
rewritecond %{http_host} ^www.domain1.com [nc]
rewriterule ^(.*)$ http://www.domain2.com/$1 [r=301,nc]
感谢shiny的反复提示,ASP脚本实现301跳转的方法:
<%
if request.ServerVariables("HTTP_HOST")="domain1.com" or request.ServerVariables("HTTP_HOST")="www.domain1.com" then
if Request.ServerVariables("QUERY_STRING")<>"" then p="?"
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.domain2.com"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")
Response.End
end if
%>
if request.ServerVariables("HTTP_HOST")="domain1.com" or request.ServerVariables("HTTP_HOST")="www.domain1.com" then
if Request.ServerVariables("QUERY_STRING")<>"" then p="?"
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.domain2.com"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")
Response.End
end if
%>
相关文章
史上最强的SEOer
利用Google高级搜索功能做SEO调研
AKCMS — 适合SEO的小型CMS(内部开发版已提供下载)
Google Sitelinks研究分析
Google要为百度谋福利?
回首2007展望2008
WordPress SEO Tips For Beginners
SEO之大腕篇
TechTarget网站优化(SEO)中的"Tag“化表现
远离自动产生的链接
史上最强的SEOer
利用Google高级搜索功能做SEO调研
AKCMS — 适合SEO的小型CMS(内部开发版已提供下载)
Google Sitelinks研究分析
Google要为百度谋福利?
回首2007展望2008
WordPress SEO Tips For Beginners
SEO之大腕篇
TechTarget网站优化(SEO)中的"Tag“化表现
远离自动产生的链接

Matt Cutts给Wordpress博客系统的SEO建议
WordPress SEO Tips For Beginners
2007/08/13 13:35 | by



一、if request.ServerVariables("HTTP_HOST")="aftea.cn" or request.ServerVariables("HTTP_HOST")="
当发现客户是用旧域名来访问网站的,则进入转向流程
二、if Request.ServerVariables("QUERY_STRING")<>"" then p="?"
Response.Status="301 Moved Permanently"
当发现页面是含参数的,则加入“?”
发送转向的HTTP状态码301
三、Response.AddHeader "Location","http://www.aftea.com.cn"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")
当不含参数时,变量P和REQUEST.ServerVariables都为空
当含有参数时,则生成被请求页的的相对URL。。。。这样就达到了一种效果:无论客户访问哪一页,都会自动转向到另一个页面的同一文件里且包含同一个参数。
举例:www.aftea.cn/go.asp?id=1383
会自动301到www.aftea.com.cn/go.asp?id=1383;
www.aftea.cn/xiaoshuo.asp会自动301到
www.aftea.com.cn/xiaoshuo.asp
是这个意思吧?
我发现重新修改发表的内容时,博客就会在"前加上斜杠,估计是为了防止跨站漏洞的出现~
一、if request.ServerVariables("HTTP_HOST")="aftea.cn" or request.ServerVariables("HTTP_HOST")="
当发现客户是用旧域名来访问网站的,则进入转向流程
二、if Request.ServerVariables("QUERY_STRING")<>"" then p="?"
Response.Status="301 Moved Permanently"
当发现页面是含参数的,则加入“?”
发送转向的HTTP状态码301
三、Response.AddHeader "Location","http://www.aftea.com.cn"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")
当不含参数时,变量P和REQUEST.ServerVariables都为空
当含有参数时,则生成被请求页的的相对URL。。。。这样就达到了一种效果:无论客户访问哪一页,都会自动转向到另一个页面的同一文件里且包含同一个参数。
举例:http://www.aftea.cn/go.asp?id=1383
会自动301到http://www.aftea.com.cn/go.asp?id=1383;
http://www.aftea.cn/xiaoshuo.asp会自动301到
http://www.aftea.com.cn/xiaoshuo.asp
是这个意思吧?
我发现重新修改发表的内容时,博客就会在"前加上斜杠,估计是为了防止跨站漏洞的出现~
这个是大致,一般写在APP_CODE目录里然后在global.asaxprotected void Application_BeginRequest(Object sender,EventArgs e)里调用,可以用刚才的方法得到请求文件的路径,然后重写。
注意,老黑的博客会在双引号前加 \
ASP.NET也是支持ASP方式的添加HTTP头。
其实PHP也是这样添加头的,虽然实现地不是很完美,但是绝对是灵活、通用的。
以下是ASP.NET里的重写的调用方法的示例,可以根据自己的需求来修改。
string URL=@\"/shiny/d*.aspx$\";
Regex regex = new Regex (URL,RegexOptions.IgnoreCase);
if(regex.IsMatch(HttpContext.Current.Request.Path))
{
Regex aregex=new Regex(@\"/shiny/(?<id>d*).aspx$\");
Match m=aregex.Match(HttpContext.Current.Request.Path);
HttpContext.Current.Response.Write(HttpContext.Current.Request.Path);
HttpContext.Current.RewritePath(regex.Replace(HttpContext.Current.Request.Path,\"/i.aspx?id=\"+m.Groups[\"id\"].Value));
}
“注意,老黑的博客会在双引号前加 \”
这句话不太明白
asp.net2.0比较方便。
而ASP就比较痛苦了~~一般弄个虚拟主机都不允许。
刚刚稍稍把代码完善一下,一般加在数据库连接文件里就可以了,贴出来:
<%
if request.ServerVariables("HTTP_HOST")="aftea.cn" or request.ServerVariables("HTTP_HOST")="www.aftea.cn" then
if Request.ServerVariables("QUERY_STRING")<>"" then p="?"
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.aftea.com.cn"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")
Response.End
end if
%>
效果演示:aftea.cn是我的旧域名,放弃不用,新域名是aftea.com.cn
有问题请邮件dj#aftea.cn
但是是跳转首页,还得抽空对内页进行处理。
我的代码如下:(跳转到新域名首页)
<%
if request.ServerVariables("HTTP_HOST")="aftea.cn" or request.ServerVariables("HTTP_HOST")="www.aftea.cn" then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.aftea.com.cn/"
Response.End
end if
%>
联系邮件dj#aftea.cn
可以提供给你们完整版本
http://sheawey.com/blog/baidu-preferred-domain.html
http://sheawey.com/blog/baidu-preferred-domain.html
可以探讨一下。
例如下面这段apache rewriting rule就是很典型的示范:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]