urlrewritingnet 域名http状态302 问题(转)

UrlRewritingNet is an Url rewriting tool for ASP .Net and Elmahis a module for logging unhandled errors.

UrlRewritingNet can set a default location for “directory” requests via defaultPage property in <urlrewritingnet> section. When a file without extension is requested the defaultPage value is appended to the original URL.

Elmah provides a handler for getting the errors summary, usually called elmah.axd. This handler also responds to the followings requests:

/elmah.axd/rss – RSS errors list feed

/elmah.axd/digestrss – RSS digest

/elmah.axd/download –  comma separated errors list

/elmah.axd/about – about page

/elmah.axd/stylesheet – the stylesheet used

/elmah.axd/detail?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – html single error summary

/elmah.axd/xml?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – xml single  error summary

/elmah.axd/json?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – json single error summary

These requests are like a request for a file with no extension. This is why the  UrlRewritingNet adds the defaultPage value which leads to the 404 Http response. First sign of this behavior is when the generated  html does not contain any CSS style applied.

To fix this situation I simply removed the defaultPage attribute from <urlrewritingnet>.

Now if the default page is not set on IIS, then will get an error when the web site is accessed only by the domain name. This situation was handled by  UrlRewritingNet, but in not a proper way, because it returns a 302 Http response(302 Found) with the location set by the defaultPage attribute value, and I think is not the best solution to give the first page when is about SEO. The drawback when the defaultPage attribute is removed is when the directories in the web site are accessed, it will give a 403.14 – Forbidden(IIS 7). But this can be handled by UrlRewritingNet using a custom UrlRewritingProvider.

During developing this custom provider I noticed it would be better to set and the HttpStatus when the url‘s are rewriten. So I added a new attribute to the <add/> node called httpStatusCode.

Creating a new UrlRewriterNet is very simple:

public class RootProvider : UrlRewritingProvider

{

public override RewriteRule CreateRewriteRule()

{

return new RootRule();

}

}

The class RootRule does all the logic:

public class RootRule : RewriteRule

{

public override void Initialize(UrlRewritingNet.Configuration.RewriteSettings rewriteSettings)

{

base.Initialize(rewriteSettings);

VirtualUrl = rewriteSettings.GetAttribute("virtualUrl", "");

DestinationUrl = rewriteSettings.GetAttribute("destinationUrl", "");

HttpStatusCode = rewriteSettings.GetAttribute("httpStatusCode", "");

}

public override bool IsRewrite(string requestUrl)

{

return requestUrl == VirtualUrl;

}

public override string RewriteUrl(string url)

{

if (!String.IsNullOrEmpty(HttpStatusCode))

{

HttpStatusCodeHandler handler = null;

switch (HttpStatusCode)

{

case "404" :

handler = new _404Handler(DestinationUrl);

break;

case "301":

handler = new _301Handler(DestinationUrl);

break;

case "302":

handler = new _302Handler(DestinationUrl);

break;

default:

handler = new NotImplementedHandler(DestinationUrl);

break;

}

handler.Execute();

return null;

}

return DestinationUrl;

}

public string VirtualUrl { get; set; }

public string DestinationUrl { get; set; }

public string HttpStatusCode { get; set; }

}

The  RootRule class instantiates a specific handler, depending by the http status code.

I created a base class to define the model of how a status code could be handled.

public class HttpStatusCodeHandler {

protected string destinationUrl;

protected HttpStatusCodeHandler() { }

public HttpStatusCodeHandler(string DestinationUrl) {

destinationUrl = DestinationUrl;

}

public virtual void Execute() {

throw new NotImplementedHttpStatusException();

}

}

For sample when a directory is accessed it can be used a 404 response.

public sealed class _404Handler : HttpStatusCodeHandler

{

private _404Handler() { }

public _404Handler(string DestinationUrl) : base(DestinationUrl) { }

public override void Execute()

{

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Status = "404 Not Found";

HttpContext.Current.Response.StatusDescription = "Not Found";

HttpContext.Current.Response.Write(File.ReadAllText(HttpContext.Current.Server.MapPath(destinationUrl)));

HttpContext.Current.Response.End();

}

}

The 302 and 301 reponses needs to add the Location in the header response. The location contains the new URL where the old resource exists now.

public sealed class _302Handler : HttpStatusCodeHandler

{

private _302Handler() { }

public _302Handler(string DestinationUrl) : base(DestinationUrl) { }

public override void Execute()

{

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Status = "302 Found";

HttpContext.Current.Response.StatusDescription = "Found";

HttpContext.Current.Response.AddHeader("Location", destinationUrl);

HttpContext.Current.Response.End();

}

}

public sealed class _301Handler : HttpStatusCodeHandler

{

private _301Handler() { }

public _301Handler(string DestinationUrl) : base(DestinationUrl) { }

public override void Execute()

{

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Status = "301 Moved Permanently";

HttpContext.Current.Response.StatusDescription = "Moved Permanently";

HttpContext.Current.Response.AddHeader("Location", destinationUrl);

HttpContext.Current.Response.End();

}

}

public sealed class NotImplementedHandler : HttpStatusCodeHandler

{

private NotImplementedHandler() { }

public NotImplementedHandler(string DestinationUrl) : base(DestinationUrl) { }

public override void Execute()

{

throw new NotImplementedHttpStatusException();

}

}

public class NotImplementedHttpStatusException : Exception

{

public override string Message

{

get

{

return "NotIplementedHttpStatusException";

}

}

}

Now in the RewriteRule section I define some rules:

to define a default page when the folder “products” is accessed

<add name="products" virtualUrl="/products/" destinationUrl="/products/latest_products.asp" httpStatusCode="302"rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" provider="RootProvider"/>

to say that a page is permanently moved and the new location is other page

<add name="about" virtualUrl="/pages.asp?func=get_content&amp;page_id=1" destinationUrl="/about.asp" httpStatusCode="301"rewriteUrlParameter="IncludeQueryStringForRewrite" ignoreCase="true" provider="RootProvider"/>

These redirects are very helpful when you want to keep the search engines rankings.

原文链接:http://csharpin.blogspot.com/2009/03/using-urlrewritingnet-and-elmah.html

时间: 2024-10-12 21:37:38

urlrewritingnet 域名http状态302 问题(转)的相关文章

[CloudXNS经验分享]修改NS,域名接管状态为×时不着急

在域名解析过程中,有些用户和我一样,总觉得接管状态从×变为√等得太久. 例如,在CloudXNS中,看到下面的红叉题主总有种啥事也不干一直等着看着它变绿的冲动. 其实冷静下来,出现红叉可能并不是域名解析商一方面的问题,甚至有时候是我自己的失误. 我在CloudXNS中添加过不少域名,在一开始不太懂的时候遇到过几次坑,现分享给大家,希望对大家有所帮助. 一.域名本身托管出错 1.NS记录未修改 NS服务器,即域名服务器,用来指定该域名由哪个DNS服务器来进行解析.只有将对应域名的NS在注册商处更改

域名状态:运营商设置了客户禁止删除保护?过期域名也不能注册为什么?

有的时候,我们打开一个网站.有的时候打不开.这个时候我们在站长工具里面查询,会有域名状态:运营商设置了客户禁止删除保护信息.这个状态对客户来说还是比较好的,以免客户一不小心把域名删了.域名过期后会依次经过以下几个阶段后才可以注册的:1. “续费宽限期”:普遍是30天,这期间是可以正常续费的.如果过了这时间还没续费,那域名就会进入下个阶段:2. “赎回期”:这时域名的状态会显示为redemption period,如果这个期间想要回域名,就需要向注册商支付高达几百或者上千元的赎回费了,这个期间也是

微信域名安全检测工具是如何检测域名状态的

针对微信转发分享链接等过程中,域名被QQ管家云安全过滤拦截而无法正常浏览, 例如该网页包含诱导分享内容,被多人投诉等. 本平台提供微信域名拦截(停止访问)监测接口服务, 我们的监控系统能快速及时地把已被拦截的域名通知对方,为了灵活性地与第三方平台无缝对接特别地提供专业API接口实时监控着域名的状态, 用户可通过此接口回调数据快速切换有效域名,大大减少人力物理给平台正常运营节省人力物力财力. 文档:最简单的 GET 接口调用方式 API 响应:毫秒级响应效率,100%准确率 API 网关:高性能.

Nginx 301重定向域名

为何要使用301重定向 在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名.网页的扩展名改变.网站域名改变等.如果不做重定向,用户的收藏和搜索引擎数据库中的旧地址只能让访客得到一个404错误信息页面,访问流量白白丧失.不仅如此,之前该页面的一切积累(比如PR值)就都白费了. 301重定向不仅能使页面实现自动跳转,对于搜索引擎来说,也可能可以传递PR值. nginx重定向规则详细介绍 http://www.jefflei.com/post/1015.html rewrite命令n

LAMP(5)域名跳转、Apache访问日志、访问日志不记录静态文件、访问日志切割

                            域名跳转    SEO(Search Engine Optimization)搜索引擎优化是一种利用搜索引擎的搜索规则来提高目前网站在有关搜索引擎内的自然排名的方式.SEO的目的理解是:为网站提供生态式的自我营销解决方案,让网站在行业内占据领先地位,从而获得品牌收益   一个网站是通过域名来判断的.   域名跳转:定义一个状态码,301永久重定向(通常都是同301,会降低原来域名权重,把权重定义到新的域名.)                

转帖:HttpStatusCode状态说明C#版

Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.SwitchingProtocols 指示正在更改协议版本或协议. OK 等效于 HTTP 状态 200.OK 指示请求成功,且请求的信息包含在响应中.这是最常接收的状态代码. Created 等效于 HTTP 状态 201.Created 指示请求导致在响应被发送前创建新资源. Accepted 等效于 HTTP 状态 202.A

从国内向海外转移域名经验谈

本站的域名imysql.com最早是从一位网友手里买来的,当时我还是国内"注明"的域名注册商X资源的代理商(历史原因,别人注册的代理权转给我),因此就将该域名从海外转回国内托管. 随着手头域名的减少,我这个代理商的注册/续费价格也水涨船高,于是打算转入我在moniker.com的账号里. 步骤一:申请转出 在X资源代理商管理后台,选择"客户服务" => "有问必答",提交相应的域名转出申请,例如: 主题:xxx.com 域名申请转出 问题分

nginx的301与302如何配置

转自:http://blog.sina.com.cn/s/blog_5d73ba76010145rr.html 首先看一个完整代码示例,关于nginx 301 302跳转的. 301跳转设置: server {listen 80;server_name 123.com;rewrite ^/(.*) http://456.com/$1 permanent;access_log off;} 302跳转设置: server {listen 80;server_name 123.com;rewrite

基于微信的域名交易系统(数据库表-修订版)

//用户表中增加了“创建时间”user 表U_id INT 8 用户idU_name VARCHAR 50 用户名U_mail VARCHAR 50 用户主邮箱U_comment VARCHAR 100 用户描述U_vip TINYINT 4 vip tagU_creatime DATATIME 创建用户时间U_frozen DECIMAL 8,2 冻结保证金 mail 表M_id INT 8 邮箱idM_name VARCHAR 50 邮箱名M_owner INT 8 邮箱属主 domain