Url rewriter是个好东西,blog也使用了这个东西
2007-03-13 14:06
531 查看
Url rewriter是个好东西,blog也使用了这个东西
Download source - 8 Kb
Introduction
One of the most popular extensions to the Apache webserver has been
you could provide a filter which accepts URLs such as
and it will silently perform a server-side redirect to the first URL. In this way, the real URL could be hidden, providing an obfuscated facade to the web page. The benefits are easier to remember URLs and increasing the difficulty of hacking a website.
As more and more websites are being rewritten with ASP.NET, the old sites which had been indexed by google and linked from other sites are lost, inevitably culminating in the dreaded 404 error. I will show how legacy ASP sites can be upgraded to ASP.NET, while maintaining links from search engines.
ASP.NET support for URL Rewriting
ASP.NET provides very limited support out of the box. In fact, it's support is down to a single method:
which should be called during the
The storage location for all ASP.NET Configuration information is the
Notice how we have to escape the period in the url element such as 'show/.asp'. This is a Regular Expression escape and it's a small price to pay for the flexibility of regular expressions. These also show how we set-up a capturing expression using (.*) in the <url> element and refer to that capture in the <rewrite> element with $1
Configuration Section Handlers
.Net's configuration mechanism requires us to write code as a "handler" for this section. Here's the code for that:
This section handler specifies that for every section called "urlrewrites", there is a class called
A section handler is defined as a class which implements the IConfigurationSectionHandler interface. This has one method,
Initiating the rewrite process
Coming back to actually rewriting the URL, as I said earlier, we need to do something in the
which calls the static method Process() on the Rewriter class. Process() first obtains a reference to the configuration section handler (which happens to be an instance of the current class) and then delegates most of the work to
Installing the sample code
Extract the code into a URLRewriter folder, then turn this into a virtual directory using the Internet Information Services MMC control panel applet. Compile the code use the 'Make Rewriter.bat' batch script into the bin sub-folder. Then add bin/ThunderMain.URLRewriter.dll to the Global Assembly Cache by copying and pasting the dll into %WINDIR%/assembly using Windows Explorer. Finally, navigate to http://localhost/URLRewriter/default.aspx and try the demo URLs listed.
None will actually work because there's one last thing we have to be aware of...
Finally
There's one major caveat with all this. If you want to process a request with a file extension other than .aspx such as .asp or .html, then you need to change IIS to pass all requests through to the ASP.NET ISAPI extension. Unfortunately, you will need physical access to the server to perform this, which prevents you from simply XCOPY deploying your code to an ISP.
We've added the HEAD, GET and POST verbs to all files with .* file extension (ie all files) and mapped those to the ASP.NET ISAPI extension - aspnet_isapi.dll.
The complete range of mappings, including the new .* mapping.
posted on 2004-06-02 09:48 edobnet 阅读(689) 评论(10) 编辑 收藏
评论
mmmm,的确有点意思
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:15
我通常写一个httphandler配合URLRewrite做url参数隐藏
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:22
In my homepage
===
===
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:37
这里在windows server 2003下就不行了呀
Extension: 这里输入.*的话,出现错误的扩展名格式
该如何解决
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:44
通过“添加”按钮右下方的“插入”按钮。
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 12:31
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-03 11:29
我试不出来:(
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-07 15:50
自己写的一个方法:
global.asax.cs文件
[align=left] protected void Application_BeginRequest(Object sender, EventArgs e) [/align]
[align=left] { [/align]
[align=left] try [/align]
[align=left] { [/align]
[align=left] string path=Server.MapPath("~/ReWriter.config"); [/align]
[align=left] XPathDocument myXPathDocument = new XPathDocument(path); [/align]
[align=left] XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator(); [/align]
[align=left] [/align]
[align=left] XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select ("//rule"); [/align]
[align=left] System.Text.RegularExpressions.Regex oReg; [/align]
[align=left] string ReWriteUrl; [/align]
[align=left] [/align]
[align=left] while (myXPathNodeIterator.MoveNext()) [/align]
[align=left] { [/align]
[align=left] //oReg=new Regex(oNode.SelectSingleNode("url/text()").Value); [/align]
[align=left] XPathNavigator nav2 = myXPathNodeIterator.Current.Clone(); [/align]
[align=left] string oldString="",newString=""; [/align]
[align=left] XPathNodeIterator it2 = nav2.Select("old"); [/align]
[align=left] while(it2.MoveNext()) [/align]
[align=left] { [/align]
[align=left] oldString = it2.Current.Value; [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] it2 = nav2.Select("new"); [/align]
[align=left] while(it2.MoveNext()) [/align]
[align=left] { [/align]
[align=left] newString = it2.Current.Value; [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] if(oldString != "" && newString != "") [/align]
[align=left] { [/align]
[align=left] oReg = new System.Text.RegularExpressions.Regex(oldString); [/align]
[align=left] if(oReg.IsMatch(Request.Url.ToString())) [/align]
[align=left] { [/align]
[align=left] ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString); [/align]
[align=left] HttpContext.Current.RewritePath(ReWriteUrl); [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] } [/align]
[align=left] } [/align]
[align=left] [/align]
[align=left] } [/align]
[align=left] catch [/align]
[align=left] { [/align]
[align=left] } [/align]
[align=left] [/align]
}
ReWriter.config内容(配制ReWriter信息)
[align=left]<?xmlversion="1.0"encoding="utf-8"?> [/align]
[align=left]<ReWriterUrls> [/align]
[align=left] <rule> [/align]
[align=left] <old>(.*)/TestUrlRe/file(.*)/(.*)/.html</old> [/align]
[align=left] <new>../WebForm1.aspx?id=$2&type=$3</new> [/align]
[align=left] </rule> [/align]
[align=left] <rule> [/align]
[align=left] <old>(.*)/TestUrlRe/t(.*)/(.*)/.html</old> [/align]
[align=left] <new>../WebForm1.aspx?tid=$2&ttype=$3</new> [/align]
[align=left] </rule> [/align]
</ReWriterUrls>
# re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 10:50 unruledboy(灵感之源)
Download source - 8 Kb
Introduction
One of the most popular extensions to the Apache webserver has been
mod_rewrite- a filter which rewrites URLs. For example, instead of a URL such as
http://www.apache.org/BookDetails.pl?id=5
you could provide a filter which accepts URLs such as
http://www.apache.org/Book/5.html
and it will silently perform a server-side redirect to the first URL. In this way, the real URL could be hidden, providing an obfuscated facade to the web page. The benefits are easier to remember URLs and increasing the difficulty of hacking a website.
Mod_rewritebecame very popular and grew to encompass a couple of other features not related to URL Rewriting, such as caching. This article demonstrates URL Rewriting with ASP.NET, whereby the requested URL is matched based on a regular expression and the URL mappings are stored in the standard ASP.NET
web.configconfiguration file. ASP.NET includes great caching facilities, so there's no need to duplicate
mod_rewrite's caching functionality.
As more and more websites are being rewritten with ASP.NET, the old sites which had been indexed by google and linked from other sites are lost, inevitably culminating in the dreaded 404 error. I will show how legacy ASP sites can be upgraded to ASP.NET, while maintaining links from search engines.
ASP.NET support for URL Rewriting
ASP.NET provides very limited support out of the box. In fact, it's support is down to a single method:
void HttpContext.RewritePath(string path)
which should be called during the
Application_BeginRequest()event in the
Global.asaxfile. This is fine as long as the number of URLs to rewrite is a small, finite, managable number. However most ASP sites are in some way dynamic, passing parameters in the Query String, so we require a much more configurable approach.
The storage location for all ASP.NET Configuration information is the
web.configfile, so we'd really like to specify the rewrites in there. Additionally, .Net has a fast regular expression processor, giving free and fast search and replace of URLs. Let's define a section in the
web.configfile which specifies those rewrites:
<configuration> <system.web> <urlrewrites> <rule> <url>/urlrewriter/show/.asp</url> <rewrite>show.aspx</rewrite> </rule> <rule> <url>/urlrewriter/wohs/.asp</url> <rewrite>show.aspx</rewrite> </rule> <rule> <url>/urlrewriter/show(.*)/.asp</url> <rewrite>show.aspx?$1</rewrite> </rule> <rule> <url>/urlrewriter/(.*)show/.html</url> <rewrite>show.aspx?id=$1&cat=2</rewrite> </rule> <rule> <url>/urlrewriter/s/h/o/w/(.*)/.html</url> <rewrite>/urlrewriter/show.aspx?id=$1</rewrite> </rule> </urlrewrites> </system.web> </configuration>
Notice how we have to escape the period in the url element such as 'show/.asp'. This is a Regular Expression escape and it's a small price to pay for the flexibility of regular expressions. These also show how we set-up a capturing expression using (.*) in the <url> element and refer to that capture in the <rewrite> element with $1
Configuration Section Handlers
.Net's configuration mechanism requires us to write code as a "handler" for this section. Here's the code for that:
<configuration> <configSections> <sectionGroup name="system.web"> <section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/> </sectionGroup> </configSections> </configuration>
This section handler specifies that for every section called "urlrewrites", there is a class called
ThunderMain.URLRewriter.Rewriterwhich can be found in the ThunderMain.URLRewriter.dll assembly with the given public key token. The public key token is required because this assembly has to be placed into the GAC and therefore given a strong name.
A section handler is defined as a class which implements the IConfigurationSectionHandler interface. This has one method,
Create(), which should be implemented, and in our code that is very simple. It merely stores the urlrewrites element for later use:
public object Create(object parent, object configContext, XmlNode section) { _oRules=section; return this; }
Initiating the rewrite process
Coming back to actually rewriting the URL, as I said earlier, we need to do something in the
Application_BeginRequest()event in Global.asax - we just delegate this to another class:
protected void Application_BeginRequest(Object sender, EventArgs e){ ThunderMain.URLRewriter.Rewriter.Process(); }
which calls the static method Process() on the Rewriter class. Process() first obtains a reference to the configuration section handler (which happens to be an instance of the current class) and then delegates most of the work to
GetSubstitution()- an instance method of this class.
public static void Process() { Rewriter oRewriter= (Rewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites"); string zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path); if(zSubst.Length>0) { HttpContext.Current.RewritePath(zSubst); } }
GetSubstitution()is just as simple - iterating through all possible URL Rewrites to see if one matches. If it does, it returns the new URL, otherwise it just returns the original URL:
public string GetSubstitution(string zPath) { Regex oReg; foreach(XmlNode oNode in _oRules.SelectNodes("rule")) { oReg=new Regex(oNode.SelectSingleNode("url/text()").Value); Match oMatch=oReg.Match(zPath); if(oMatch.Success) { return oReg.Replace(zPath,oNode.SelectSingleNode("rewrite/text()").Value); } } return zPath; }
Installing the sample code
Extract the code into a URLRewriter folder, then turn this into a virtual directory using the Internet Information Services MMC control panel applet. Compile the code use the 'Make Rewriter.bat' batch script into the bin sub-folder. Then add bin/ThunderMain.URLRewriter.dll to the Global Assembly Cache by copying and pasting the dll into %WINDIR%/assembly using Windows Explorer. Finally, navigate to http://localhost/URLRewriter/default.aspx and try the demo URLs listed.
None will actually work because there's one last thing we have to be aware of...
Finally
There's one major caveat with all this. If you want to process a request with a file extension other than .aspx such as .asp or .html, then you need to change IIS to pass all requests through to the ASP.NET ISAPI extension. Unfortunately, you will need physical access to the server to perform this, which prevents you from simply XCOPY deploying your code to an ISP.
We've added the HEAD, GET and POST verbs to all files with .* file extension (ie all files) and mapped those to the ASP.NET ISAPI extension - aspnet_isapi.dll.
The complete range of mappings, including the new .* mapping.
posted on 2004-06-02 09:48 edobnet 阅读(689) 评论(10) 编辑 收藏
评论
mmmm,的确有点意思
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:15
我通常写一个httphandler配合URLRewrite做url参数隐藏
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:22
In my homepage
===
===
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:37
这里在windows server 2003下就不行了呀
Extension: 这里输入.*的话,出现错误的扩展名格式
该如何解决
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 11:44
通过“添加”按钮右下方的“插入”按钮。
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 12:31
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-03 11:29
我试不出来:(
re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-07 15:50
自己写的一个方法:
global.asax.cs文件
[align=left] protected void Application_BeginRequest(Object sender, EventArgs e) [/align]
[align=left] { [/align]
[align=left] try [/align]
[align=left] { [/align]
[align=left] string path=Server.MapPath("~/ReWriter.config"); [/align]
[align=left] XPathDocument myXPathDocument = new XPathDocument(path); [/align]
[align=left] XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator(); [/align]
[align=left] [/align]
[align=left] XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select ("//rule"); [/align]
[align=left] System.Text.RegularExpressions.Regex oReg; [/align]
[align=left] string ReWriteUrl; [/align]
[align=left] [/align]
[align=left] while (myXPathNodeIterator.MoveNext()) [/align]
[align=left] { [/align]
[align=left] //oReg=new Regex(oNode.SelectSingleNode("url/text()").Value); [/align]
[align=left] XPathNavigator nav2 = myXPathNodeIterator.Current.Clone(); [/align]
[align=left] string oldString="",newString=""; [/align]
[align=left] XPathNodeIterator it2 = nav2.Select("old"); [/align]
[align=left] while(it2.MoveNext()) [/align]
[align=left] { [/align]
[align=left] oldString = it2.Current.Value; [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] it2 = nav2.Select("new"); [/align]
[align=left] while(it2.MoveNext()) [/align]
[align=left] { [/align]
[align=left] newString = it2.Current.Value; [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] if(oldString != "" && newString != "") [/align]
[align=left] { [/align]
[align=left] oReg = new System.Text.RegularExpressions.Regex(oldString); [/align]
[align=left] if(oReg.IsMatch(Request.Url.ToString())) [/align]
[align=left] { [/align]
[align=left] ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString); [/align]
[align=left] HttpContext.Current.RewritePath(ReWriteUrl); [/align]
[align=left] break; [/align]
[align=left] } [/align]
[align=left] } [/align]
[align=left] } [/align]
[align=left] [/align]
[align=left] } [/align]
[align=left] catch [/align]
[align=left] { [/align]
[align=left] } [/align]
[align=left] [/align]
}
ReWriter.config内容(配制ReWriter信息)
[align=left]<?xmlversion="1.0"encoding="utf-8"?> [/align]
[align=left]<ReWriterUrls> [/align]
[align=left] <rule> [/align]
[align=left] <old>(.*)/TestUrlRe/file(.*)/(.*)/.html</old> [/align]
[align=left] <new>../WebForm1.aspx?id=$2&type=$3</new> [/align]
[align=left] </rule> [/align]
[align=left] <rule> [/align]
[align=left] <old>(.*)/TestUrlRe/t(.*)/(.*)/.html</old> [/align]
[align=left] <new>../WebForm1.aspx?tid=$2&ttype=$3</new> [/align]
[align=left] </rule> [/align]
</ReWriterUrls>
# re: 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个) 2004-06-02 10:50 unruledboy(灵感之源)
相关文章推荐
- 发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个)
- mysql插入int字段可以使用单引号包含?今天改东西无意发现时间字段成int类型了,而且update这个字段用单引号包含,于是在MySQL上测试下能运行,求解
- 【推荐】C语言深度剖析,这个blog的作者总结的这些东西还是挺好的,下载也是免费的
- [BLOG] 这几天对trackback这个东西感兴趣了
- 刚申请这个Blog,怎么也要写点东西
- 不使用这个BLOG了...
- CSDN的BLOG可以正常使用了,原来是因为这个原因啊!
- 这是第一次在这个blog上写东西
- 关于这个blog使用的问题?
- 开始使用这个感觉上很专业的blog~
- 大家好 这个事我的BLOG 站点 欢迎大家 訪问和公布文章技术的 和评论 交流技术使用
- mencoder mplayer 命令的使用(安装mplayer 也会安装这个东西)视频音频解码编码转码
- 又闲置这个blog一年多,现在再放点东西上来吧。。
- fckeditor的基本测试--一直都不会使用这个东西。
- 这个blog功能还算不错,还可以隐藏一些属于自己的东西。
- 今天开始使用这个blog来记录我的学习中遇到的问题吧
- 申请这个BLOG好长时间了,现在开始进行使用,呵呵!
- 今天第一次使用这个Blog
- 第一次使用这个blog
- 正式开始使用这个blog了