您现在的位置是:网站首页 >> 代码素材
代码素材

联 系 人:李总
联系电话:13759574266
在线 QQ:89417157
邮箱:13759574266@qq.com
微信号:ynlongtou
地址:昆明市滇缅大道旁昆建路5号108智库空间A座4楼

代码素材

ASP代码实现301重定向及带参数的方法(亲测多种完全解决方法)

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

  或者由于改版需要,或有多个域名需要做301重定向,还有之前网站的一个栏目需要做301重定向,但是原来的很多网址都是有参数的,例如TAG标签,形式如:list.asp?q=%C1%D2%BB%F0%CD%F8。

  研究了一下,解决了301重定向带参数的问题,特来向大家分享,欢迎朋友多支持云南龙头科技有限公司

  将以下代码放到ASP公用文件顶部。

  提供多种解决方法,具体如下:

一、ASP代码实现一个域名不带www的301重定向方法,代码如下:

<% 

website=request.ServerVariables("Server_name") 

'获取当前访问的域名 

if website="ynlongtou.com" then 

'判断如果你的域名如果是ynlongtou.com 

Response.Status="301 Moved Permanently" 

Response.AddHeader "Location","http://www.ynlongtou.com/" 

'做301重定向到www.ynlongtou.com 

end if 

%>


二、ASP代码实现多个域名301重定向的方法,代码如下:

<%

website=request.ServerVariables("Server_name")

'获取当前访问的域名

if website="www.kmgmw.cn" or website="kmgmw.cn" or website="kmgm.com.cn" then

'判断如果你的域名如果是www.kmgmw.cn or kmgmw.cn or kmgm.com.cn

Response.Status="301 Moved Permanently"

Response.AddHeader "Location","http://www.kmgm.com.cn/"

'做301重定向到www.kmgm.com.cn

end if

%>


三、ASP代码实现一个域名301重定向及带参数的方法,代码如下:

<% 

if request.ServerVariables("HTTP_HOST")="ynlongtou.com" Then 

if Request.ServerVariables("QUERY_STRING")<>"" Then 

p="?" 

Response.Status="301 Moved Permanently" 

Response.AddHeader "Location","http://www.ynlongtou.com"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING") 

Response.End 

else 

Response.Status="301 Moved Permanently" 

Response.AddHeader "Location","http://www.ynlongtou.com/" 

Response.End 

end if 

end if 

%>


四、ASP代码实现多个域名301重定向及带参数的方法,代码如下:

<% 

if request.ServerVariables("HTTP_HOST")="www.kmgmw.cn" or request.ServerVariables("HTTP_HOST")="kmgmw.cn" or request.ServerVariables("HTTP_HOST")="kmgm.com.cn"  Then 

if Request.ServerVariables("QUERY_STRING")<>"" Then 

p="?" 

Response.Status="301 Moved Permanently" 

Response.AddHeader "Location","http://www.kmgm.com.cn"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING") 

Response.End 

else 

Response.Status="301 Moved Permanently" 

Response.AddHeader "Location","http://www.kmgm.com.cn/" 

Response.End 

end if 

end if 

%>


五、搭建一个站点,配置web.config文件实现301跳转。

  已经有一个正常访问的网站,其它域名需要通过301跳转到正常访问的网站上,首先把要跳转的域名解析好,再新建一个站点,将需要跳转的域名通过配置web.config文件实现301跳转。web.config的配置文件如下:

<?xml version="1.0" ?>

<configuration>

<location allowOverride="false" inheritInChildApplications="false" path=".">

<system.webServer>

<rewrite>

<rules configSource="web_config\rewrite.config"/>

</rewrite>

<defaultDocument configSource="web_config\default.config"/>

<httpErrors configSource="web_config\httpErrors.config"/>

<handlers configSource="web_config\php.config"/>

</system.webServer>

</location>

</configuration>

  在根目录下创建web_config文件夹,在web_config文件夹再新建rewrite.config文件,rewrite.config文件的代码如下:

<?xml version="1.0" ?>

<rules>

<clear/>

<rule name="1637200018541_redirect" stopProcessing="true">

<match url="(.*)"/>

<conditions logicalGrouping="MatchAny">

<add input="{HTTP_HOST}" pattern="^ynlcpx.com"/>

<add input="{HTTP_HOST}" pattern="^www.ynlcpx.com.cn"/>

<add input="{HTTP_HOST}" pattern="^ynlcpx.com.cn"/>

<add input="{HTTP_HOST}" pattern="^www.ynlcpx.cn"/>

</conditions>

<action redirectType="Permanent" type="Redirect" url="http://www.ynlcpx.com/{R:0}"/>

</rule>

</rules>


  除了以上方法,有些虚拟主机的空间也自带有301跳转的功能。请自己尝试进行设置。

  或如果你是用宝塔面板创建的网站,宝塔也是有301跳转设置功能的。都自己尝试下。相信介绍了这么多方法,总有一种方法是适合你的。希望你能设置成功。


  以上代码为云南龙头科技有限公司技术人员亲测均正常。加入代码后看是否重定向到了301,可通过以下网址进行在线检测。

  301检测_HTTP状态查询网址:https://tool.chinaz.com/pagestatus/

  更多云南龙头科技有限公司网站案例请查看:http://www.ynlongtou.com/case.asp?id=3

  云南龙头科技有限公司凭借多年的网站建设经验,坚持以“帮助中小企业实现网络营销化”为宗旨,成功帮助多家中小企业实现互联网信息化建设,得到了客户的一致好评。如你有任何关于网站建设的疑问,请立即点击咨询云南龙头科技有限公司资深营销专家或拨打咨询热线:13759574266,我们会详细为你一一解答你心中的疑难。

云南龙头科技有限公司服务范围