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 ...
随机推荐
- python numpy array 的一些问题
1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素 ...
- matplotlib 绘制柱状图的几个例子
1 error bar #!/usr/bin/env python # a bar plot with errorbars import numpy as np import matplotlib.p ...
- Java调优之jvm和线程的内存分析
本文来源于铁木箱子的博客http://www.mzone.cc 这几天因为自己开发的一个网站在768M内存的机器上撑不起100多个用户的运行,因为每个用户启用功能后,系统将为每个用户分配8个左右的独立 ...
- unable to load default svn client
解决方法: .windows->preferences->Team->SVN->SVN接口 选择SVNKit...
- [禅悟人生]"执著"是自缚的茧
宋代苏东坡和佛印禅师是好朋友,他们习惯拿对方开玩笑.有一天,苏东坡到金山寺和佛印禅师打坐参禅,苏东坡觉得身心通畅,于是问禅师道:“禅师!你看我坐的样子怎么样?” “好庄严,像一尊佛!” 苏东坡听了非常 ...
- JAVA CAS原理、unsafe、AQS
concurrent包的实现 由于java的CAS同时具有 volatile 读和volatile写的内存语义,因此Java线程之间的通信现在有了下面四种方式: A线程写volatile变量,随后B线 ...
- LruCache--远程图片获取与本地缓存
Class Overview A cache that holds strong references to a limited number of values. Each time a value ...
- Golang连接Oracle数据库
Golang连接Oracle的库有很多,比较常见的如下: 不过,oralce 只提供了 oci8 的接口,必须通过它来调用,所以下面方案都逃不过相关设置. 1.go-db-oracle 地址: htt ...
- 用python3破解wingIDE
值得注意的是,python2的整除/在python3中变成了//,sha方法细化成了sha1和sha256,所以破解文件需要更改加密方式和整除部分的编码方式,经过修改后,这个文件可以完美演算出破解码, ...
- 数往知来C#之 String 集合 深拷与浅拷 序列化<五>
C# 基础常用string方法篇 复习. 1.引用类型与值类型 -->文件的复制与快捷方式的复制 2.垃圾回收 3.静态与非静态 -->如何定义静态成员与静态类 --> ...