网上很多301代码都是仅仅实现了网站首页的301,无法支持整站301。或者仅仅支持动态网站的301,无法支持静态网站的301,下面我将分享实现“asp整站伪静态后实现整站301”的经验。 将如下代码放在asp网站最顶部,一般放在header.asp文件的顶部即可,下列源码将以域名:www.monnai.cn、monnai.net、www.monnai.net为例,实现将这三个域名都整站301到域名monnai.cn下,请您根据根据实际需求将以上示例域名修改成自己的域名,同时replace部分也要根据实际需求做出相应修改。 Dim sername,url sername=lcase(request.ServerVariables("SERVER_NAME")) url=lcase(request.ServerVariables("URL")) if sername="www.monnai.cn" or sername="monnai.net" or sername="www.monnai.net" then if url="/index.asp" or url="/index.html" then Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://monnai.cn/" else Dim str,rewurl str=lcase(request.ServerVariables("HTTP_HOST")) rewurl=lcase(request.ServerVariables("HTTP_X_REWRITE_URL")) str=replace(str,"www.","") str=replace(str,"net","cn") Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://"+str+rewurl Response.End end if end if 为了不让http://monnai.cn/index.asp或者http://monnai.cn/index.html的情况出现,在第一个if语句中我做了判断,至于后面的replace部分应该很好理解,因为示例中是不使用www.形式域名的,所以直接替换掉了,然后就是把net替换成cn,这里最主要的部分还是:HTTP_X_REWRITE_URL的使用,因为整站伪静态后,页面地址必须要使用HTTP_X_REWRITE_URL来获取,以上代码测试绝对可用,如有意见和需求请在页面下方评论处发表,本次经验就分享到这里,希望对大家有用……
|