问题:
如果一个网站拥有两个域名: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]


注释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]





感谢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
%>


SEO » SEO教程 | 评论(32) | 引用(0) | 阅读(7689)

shiny
2007/08/20 16:14
看来老黑还是没有很好看写的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。。。。这样就达到了一种效果:无论客户访问哪一页,都会自动转向到另一个页面的同一文件里且包含同一个参数。

举例: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



是这个意思吧?



我发现重新修改发表的内容时,博客就会在"前加上斜杠,估计是为了防止跨站漏洞的出现~
黑色梦中 回复于 2007/08/20 18:59
哈哈,是我的失误,我看明白了。你获取了URL后的脚本信息,然后传递到新域名的URL中
shiny
2007/08/20 16:13
看来老黑还是没有很好看写的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



是这个意思吧?



我发现重新修改发表的内容时,博客就会在"前加上斜杠,估计是为了防止跨站漏洞的出现~
shiny
2007/08/20 12:22
asp.net下的重写URL,但是不知道是不是301。。有机会用SNIFFER来看看。
这个是大致,一般写在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));
       }
黑色梦中 回复于 2007/08/20 18:54
shiny:
“注意,老黑的博客会在双引号前加 \”
这句话不太明白
shiny
2007/08/20 12:05
重写URL虽然很不错但也要服务器支持。linux环境下才能用.htaccess
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
黑色梦中 回复于 2007/08/20 14:22
ASP机制下的域名跳转比较简单,关键词是内页之间的如何实现跳转,我会EMAIL和你取得联系
shiny
2007/08/20 11:48
面临的问题正好是我面对的。我用的是ASP。
但是是跳转首页,还得抽空对内页进行处理。
我的代码如下:(跳转到新域名首页)
<%
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
可以提供给你们完整版本
sheawey Email
2007/08/17 20:00
关于把www.abc.com  转到abc.com此类的操作我曾经玩过,但是被百度惩罚了,你可以看我的博客文章即知,我现在还不能确定是为啥原因。

http://sheawey.com/blog/baidu-preferred-domain.html

http://sheawey.com/blog/baidu-preferred-domain.html

可以探讨一下。
电脑通天下 Email
2007/08/16 16:11
学习中。
dawnh Email
2007/08/13 15:33
通过判断http_host变量,将URL重写至新的host,可以从多个层面解决,不过一般来说这种程度的转向使用Web Server本身的Rewrite功能是最适合的。
例如下面这段apache rewriting rule就是很典型的示范:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
oxygen
2007/08/13 15:13
正需要这个功能。。。不知道可有大侠能解决的。。。
Cloudream
2007/08/13 14:43
引入一段js判断一下 再显示tip就可以了……
黑色梦中 回复于 2007/08/14 09:35
JS可以实现返回301状态码吗?
分页: 3/4 第一页 上页 1 2 3 4 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]