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_rewrite
became 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.config
configuration 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.asax
file. 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.config
file, 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.config
file 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.Rewriter
which 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 阅读(6505)
评论(18) 编辑 收藏 所属分类:
.Net
<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.cnblogs.com/edobnet/archive/2004/06/02/12866.html"
dc:identifier="http://www.cnblogs.com/edobnet/archive/2004/06/02/12866.html"
dc:title="发现Url rewriter是个好东西啊,blog也使用了这个东西(转一篇文章研究一个)"
trackback:ping="http://www.cnblogs.com/edobnet/services/trackbacks/12866.aspx" />
</rdf:RDF>
-->
<!--done-->
评论:
#8楼 2004-06-07 15:50 |
edobnet [未注册用户]自己写的一个方法:
global.asax.cs文件
protected void Application_BeginRequest(Object sender, EventArgs e)
{
try
{
string path=Server.MapPath("~/ReWriter.config");
XPathDocument myXPathDocument = new XPathDocument(path);
XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select ("//rule");
System.Text.RegularExpressions.Regex oReg;
string ReWriteUrl;
while (myXPathNodeIterator.MoveNext())
{
//oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
XPathNavigator nav2 = myXPathNodeIterator.Current.Clone();
string oldString="",newString="";
XPathNodeIterator it2 = nav2.Select("old");
while(it2.MoveNext())
{
oldString = it2.Current.Value;
break;
}
it2 = nav2.Select("new");
while(it2.MoveNext())
{
newString = it2.Current.Value;
break;
}
if(oldString != "" && newString != "")
{
oReg = new System.Text.RegularExpressions.Regex(oldString);
if(oReg.IsMatch(Request.Url.ToString()))
{
ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString);
HttpContext.Current.RewritePath(ReWriteUrl);
break;
}
}
}
}
catch
{
}
}
ReWriter.config内容(配制ReWriter信息)
<?xml version="1.0" encoding="utf-8" ?>
<ReWriterUrls>
<rule>
<old>(.*)/TestUrlRe/file(.*)/(.*)/.html</old>
<new>../WebForm1.aspx?id=$2&type=$3</new>
</rule>
<rule>
<old>(.*)/TestUrlRe/t(.*)/(.*)/.html</old>
<new>../WebForm1.aspx?tid=$2&ttype=$3</new>
</rule>
</ReWriterUrls>
回复引用查看
#11楼[楼主] 2005-01-17 09:00 |
edobnet你需要把后缀为.html的文件的解析器设为.net解析.
IIS管理器+web站点+右键属性+主目录+配置+添加一个和.aspx类似的解析就行了!
回复引用查看
#12楼 2005-01-18 10:27 |
汽车 [未注册用户]"你需要把后缀为.html的文件的解析器设为.net解析.
IIS管理器+web站点+右键属性+主目录+配置+添加一个和.aspx类似的解析就行了! "
不行呀,
我用的是你自己写的那个ReWriter.config
回复引用查看
<!--Beging Temp Save--> <script type="text/javascript">
function pageLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}
function handleInitializeRequest(sender, args)
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
var eid = args.get_postBackElement().id;
if (eid.indexOf("DeleteLink")>0)
{
args.get_postBackElement().innerHTML = "<font color='red'>正在删除...</font>";
}
else if (eid.indexOf("btnSubmit")>0)
{
document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
}
else if(eid.indexOf("refreshList")>0)
{
document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML="<font color='red'>正在刷新...</font>";
}
}
function TempSave(ElementID)
{
try
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
catch(ex)
{
}
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</script><!--Ene TempSave-->
分享到:
相关推荐
伪静态URL重写是通过微软公司封装好了的一个URL重写组件(UrlReswriter.jar)来实现的,导入JAR包,在WEB服务中加载伪静态地址拦截器,客户端以静态地址向服务器发起请求,服务器端的拦截器拦截客户端请求,查找XML...
urlrewrite url地址重写 郁闷,还要我写够20个字。。我倒!
C# URL重写 URL重定向 地址重写 .net源码 核心代码 简单易用源码
该文件是一个asp.neturl地址重写的简单实例.
URL重写URL重写URL重写URL重写URL重写URL重写
URL重写URL重写URL重写URL重写URL重写URL重写
URL重写URL重写URL重写URL重写URL重写URL重写
C# url重写 C# url重写 C# url重写
ASP.NET C# URL重写 地址重写 伪静态 转HTML
对url地址重写方便seo的搜索,提高网站访问的安全性,对地址栏中的地址实现隐藏。
UserId=2的效果一样,这里就用到了URL地址重写的目的。在其它动态语言如asp、JSP、PHP中要实现URL地址重写需要借助于其它的手段(有一些现成可用的用于实现URL地址重写的dll库),使用这些第三方手段时需要对IIS做些...
有例子,其中有说明注释.
URL重写DEMOURL重写DEMOURL重写DEMOURL重写DEMO
asp.net c# Url重写和无扩展名Url重写 内有所有源码 注:无扩展名Url重写,必须是虚拟目录的相对路径
Url重写,无后缀Url重写,UrlRewriter.dll
Url重写,Url重写,asp.enturl重写,c#,ASP.NETweb网站Url重写,地址栏重写
Asp.Net 做的水印图片 和 URL地址重写,如果后缀名是.aspx将它在地址栏改变为其他的后缀名显示~~如*.jsp,*.php 等!!
URLRewrite URL重写组件 里边有相关配置说明 包括了web.Config 外附一个Css
IIS URL Rewrite模块(URL 重写)
UrlReWrite(Url重写或伪静态)完美示例源码,已利用ActionlessForm解决原始地址form回传的问题 例如:/products.aspx?category=books 转换为/products/Books.aspx或/products/Books.html(需要在IIS中建立映射)...