ASP.NET在程序中设置301的方法。

因为IIS设置301需要在服务器中配置很麻烦,所以ME选择了在程序中实现。
程序中实现有个缺点就是执行效率没有在IIS服务器中速度快。

当然了,这里说的只是适合动态网站的,如果都是.html静态文件就飘过吧!

好了还是直接上代码吧:
网页首页文件index.aspx后台代码

 
  
// 判断是否是www.开头,如果不是301调整到www.域名
if ( ! System.Web.HttpContext.Current.Request.Url.ToString().StartsWith( " http://www. " ))
{
// 301 重定向到 /目录下
HttpContext.Current.Response.StatusCode = 301 ;
HttpContext.Current.Response.Status
= " 301 Moved Permanently " ;
HttpContext.Current.Response.AddHeader(
" Location " , " http://www.qinquan.org/ " );
HttpContext.Current.Response.End();
}

这里因为是我的独立站点,所以直接写www.了。如果是二级域名就需要根据需求自己修过了。


栏目页/内容页代码:

//如果url结尾不是以/符号结尾的,同样301到末尾增加/符号。
 
  
if ( ! System.Web.HttpContext.Current.Request.RawUrl.EndsWith( " / " ))
{
// 301 重定向到 /目录下
HttpContext.Current.Response.StatusCode = 301 ;
HttpContext.Current.Response.Status
= " 301 Moved Permanently " ;
HttpContext.Current.Response.AddHeader(
" Location " , System.Web.HttpContext.Current.Request.RawUrl + " / " );
HttpContext.Current.Response.End();
}

转载于:https://www.cnblogs.com/chear/archive/2011/05/09/2041496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值