灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:8210回复:0

asp页面实现301重定向方法

楼主#
更多 发布于:2013-05-13 10:10

301重定向在很多地方都需要用到,也是seo中常见的问题。比如确定首选域或更换网站域名的时候都要用到301重定向。301重定向的方法有好几种,拿ASP类网站来说有:首页301重定向和全站301重定向。

首页301重定向的方法:
 
[html]
<%
website=request.ServerVariables("Server_name")
'获取当前访问的域名
if website="zzrpjc.com" then
'判断如果你的域名如果是icoa.cn
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.zzrpjc.com/"
'做301重定向到www.atcpu.comend if
%>
<%
website=request.ServerVariables("Server_name")
'获取当前访问的域名
if website="zzrpjc.com" then
'判断如果你的域名如果是icoa.cn
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.zzrpjc.com/"
'做301重定向到www.atcpu.comend if
%>或

[html]
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.zzrpjc.com/
%>
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.zzrpjc.com/
%>
全站301重定向的方法:
[html]
<%
currentdomain= request.ServerVariables("SERVER_NAME") '获取地址栏中当前的域名。
currenturl=request.ServerVariables("url") '获取地址栏中的域名后的ASP文件名。
currentrequest=request.ServerVariables("QUERY_STRING") '获取地址栏中?后面的参数
'如果你当前浏览的是老的PR查询地址http://zzruipu.com/product_show.asp?id=395。则获得的currentdomain为zzruipu.com,currenturl为/product_show.asp,currentrequest为id=395
If currentdomain="zzruipu.com" Then '因为www.zzruipu.com和zzruipu.com是公用所有文件。所以这里加个判定,只有当当前浏览的域名是zzruipu.com的时候才做301跳转,只有才能实现就算是公用文件,一个空间绑定多个域名,实现老域名跳转到新的域名。
 
 moveurl="http://www.zzruipu.com"&turl   '重新组合为http://www.zzruipu.com/product_show.asp
 
 If currentrequest<>"" Then
  moveurl=moveurl&"?"&trequest  '因为还有参数,所以将参数也组合到新的跳转地址中去。
 End if
 '跳转。
 Response.Status="301 Moved Permanently"  '以下两句实现301永久性跳转
 Response.AddHeader "Location",moveurl
End if
%>
<%
currentdomain= request.ServerVariables("SERVER_NAME") '获取地址栏中当前的域名。
currenturl=request.ServerVariables("url") '获取地址栏中的域名后的ASP文件名。
currentrequest=request.ServerVariables("QUERY_STRING") '获取地址栏中?后面的参数
'如果你当前浏览的是老的PR查询地址http://zzruipu.com/product_show.asp?id=395。则获得的currentdomain为zzruipu.com,currenturl为/product_show.asp,currentrequest为id=395
If currentdomain="zzruipu.com" Then '因为www.zzruipu.com和zzruipu.com是公用所有文件。所以这里加个判定,只有当当前浏览的域名是zzruipu.com的时候才做301跳转,只有才能实现就算是公用文件,一个空间绑定多个域名,实现老域名跳转到新的域名。
 moveurl="http://www.zzruipu.com"&turl   '重新组合为http://www.zzruipu.com/product_show.asp
 If currentrequest<>"" Then
  moveurl=moveurl&"?"&trequest  '因为还有参数,所以将参数也组合到新的跳转地址中去。
 End if
 '跳转。
 Response.Status="301 Moved Permanently"  '以下两句实现301永久性跳转
 Response.AddHeader "Location",moveurl
End if
%>
注意:上面代码只能加在ASP文件代码中的最上面
以上方法都能实现让不带www的域名跳转到带www的域名上,也就是我们常说的301重定向。在这里lizhi125要提醒大家一下静态页面是无法实现301重定向的,也就是说代码只能加在动态页面中,最好是网站首页的开头。如,index.asp里面或者公共调用的文件(如conn.asp)里面。

喜欢0 评分0
游客

返回顶部