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&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
urlrewritingnet 域名http状态302 问题(转)的更多相关文章
- shell+curl监控网站页面(域名访问状态),并利用sedemail发送邮件
应领导要求,对公司几个主要站点的域名访问情况进行监控.下面分享一个监控脚本,并利用sendemail进行邮件发送. 监控脚本如下:下面是写了一个多线程的网站状态检测脚本,直接从文件中读出站点地址,然后 ...
- shell+curl监控网站页面(域名访问状态),并利用sendemail发送邮件
应领导要求,对公司几个主要站点的域名访问情况进行监控.下面分享一个监控脚本,并利用sendemail进行邮件发送. 监控脚本如下:下面是写了一个多线程的网站状态检测脚本,直接从文件中读出站点地址,然后 ...
- Nginx 301重定向域名
为何要使用301重定向 在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名.网页的扩展名改变.网站域名改变等.如果不做重定向,用户的收藏和搜索引擎数据库中的旧地址只能让访客得到一个4 ...
- 三、nginx301跳转302跳转
301跳转设置: server { listen 80; server_name downcc.com; rewrite ^/(.*) http://www.downcc.com/$1 permane ...
- 什么是HTTP协议?常用的状态码有哪些?
一.HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...
- 未注册wang域名批量查询工具
一.支持规则查询 可自定义生成域名进行查询,可生成任意位数的字母数字域名,根据[声母].[韵母]生成单拼,双拼,三拼等域名,还可根据字典生成,支持全拼.首拼识别,全国城市区号.城市全拼.城市首拼.热门 ...
- 包含为 HTTP 定义的状态代码的值(枚举)
using System; namespace System.Net { // 摘要: // 包含为 HTTP 定义的状态代码的值. public enum HttpStatusCode { // 摘 ...
- php调用whois接口域名查询
由两部分组成,一个index.php文件,一个whois的接口文件: <html> <head> <title>域名到期查询</title> <s ...
- C#HttpWebResponse请求常见的状态码
成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...
随机推荐
- ArcGlobe点击IGlobeServerLayer图层读取信息
ArcGISServer将点图层发布成Globe服务,AE开发中自定义识别工具,读取点数据信息. 1) 通过Locate方法获取图层对象,图层对象中的SearchOID就是你点中的要素Objectid ...
- C#的Timer
PowerCoder 原文 C#的Timer 再C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Tim ...
- [Everyday Mathematics]20150218
设 $A,B$ 是 $n$ 阶复方阵, 适合 $$\bex A^2B+BA^2=2ABA. \eex$$ 试证: 存在 $k\in\bbZ^+$, 使得 $(AB-BA)^k=0$.
- EhCache 分布式缓存/缓存集群
开发环境: System:Windows JavaEE Server:tomcat5.0.2.8.tomcat6 JavaSDK: jdk6+ IDE:eclipse.MyEclipse 6.6 开发 ...
- Effective java笔记5--通用程序设计
一.将局部变量的作用域最小化 本条目与前面(使类和成员的可访问能力最小化)本质上是类似的.将局部变量的作用域最小化,可以增加代码的可读性和可维护性,并降低出错的可能性. 使一个局部变量的作用 ...
- ASP.NET 使用application和session对象写的简单聊天室程序
ASP.Net中有两个重要的对象,一个是application对象,一个是session对象. Application:记录应用程序参数的对象,该对象用于共享应用程序级信息. Session:记录浏览 ...
- NewtonPrincipia --- 公理或运动的定律 --- 系理二
NewtonPrincipia --- 公理或运动的定律 --- 系理二 自然哲学的数学原理>公理或运动的定律>系理II 平行四边形ABCD,那么:直接的力AD由任意的力AB和BD合成,直 ...
- 推荐一款C#反编译软件(开源)
大二的时候老师要求做过一个小项目,大概4个人左右一组.当时交流不是特别到位,项目在一个同学的电脑上建成了就一直在他的电脑上(所以好东西不要烂在你的硬盘里),也不知道什么源码管理,可悲到项目做完我还没有 ...
- 数往知来C#面向对象〈三〉
C# 基础方法篇 一.复习 1)方法的重载 方法的重载并不是一个方法,实际上方法的重载就是一些不同的 方法,目的是为了方便程序员编码,所以将功能相近的方法命名相同 根据参数,编译器自动的去匹配方法体, ...
- c 按范围快速指定整数
以前用过octave, 和matlab类似的软件, 指定范围非常方便 i = 1:10:100; 就可以得到 10 20 30 ... 100 这一系列的数据, 但是在c里面, 必须手动写循环, 太 ...