It is found that URL rewriter is a good thing, and blog also uses this thing as an article to study it)

Source: Internet
Author: User
    • Download source-8 KB

Introduction

One of the most popular extensions to the Apache webserver has beenMod_rewrite-A filter which rewrites URLs. For example, instead of a URL such

Http://www.apache.org/BookDetails.pl? Id = 5

You cocould provide a filter which accepts URLs such

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 cocould 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. netWeb. configConfiguration File. ASP. NET nodes des great caching facilities, so there's no need to duplicateMod_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:

 
VoidHttpcontext. rewritepath (string path)

Which shoshould be called duringApplication_beginrequest ()Event inGlobal. 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 than able approach.

The storage location for all ASP. NET configuration information isWeb. 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 inWeb. configFile which specifies those rewrites:

 & Lt; Configuration & Gt;    & Lt; System. Web & Gt;          & Lt; Urlrewrites & Gt;              & Lt; Rule & Gt;                  & Lt; URL & Gt; /Urlrewriter/show \. asp & Lt; /URL & Gt;                  & Lt; Rewrite & Gt; Show. aspx & Lt; /Rewrite & Gt;              & Lt; /Rule & Gt;              & Lt; Rule & Gt;                  & Lt; URL & Gt; /Urlrewriter/wohs \. asp & Lt; /URL & Gt;                  & Lt; Rewrite & Gt; Show. aspx & Lt; /Rewrite & Gt;              & Lt; /Rule & Gt;              & Lt; Rule & Gt;                  & Lt; URL & Gt; /Urlrewriter/show (. *) \. asp & Lt; /URL & Gt;                  & Lt; Rewrite & Gt; Show. aspx? $1 & Lt; /Rewrite & Gt;              & Lt; /Rule & Gt;              & Lt; Rule & Gt;                  & Lt; URL & Gt; /Urlrewriter/(. *) show \. html & Lt; /URL & Gt;                 & Lt; Rewrite & Gt; Show. aspx? Id = $1 & Amp; Amp; cat = 2 & Lt; /Rewrite & Gt;              & Lt; /Rule & Gt;              & Lt; Rule & Gt;                  & Lt; URL & Gt; /Urlrewriter/S/H/O/W/(. *) \. html & Lt; /URL & Gt;                 & Lt; Rewrite & Gt; /Urlrewriter/show. aspx? Id = $1 & Lt; /Rewrite & Gt;              & Lt; /Rule & Gt;          & Lt; /Urlrewrites & Gt;      & Lt; /System. Web & Gt;  & Lt; /Configuration & Gt; 

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:

 & Lt; Configuration & Gt;      & Lt; Configsections & Gt;          & Lt; Sectiongroup name = "system. Web" & Gt;             & Lt; Section name = "urlrewrites" type = "thundermain. urlrewriter. rewriter, thundermain. urlrewriter, version = 1.0.783.30976, culture = neutral, publickeytoken = 7a95f6f4820c8dc3 "/ & Gt;          & Lt; /Sectiongroup & Gt;      & Lt; /Configsections & Gt;  & Lt; /Configuration & Gt; 

This section handler specifies that for every section called "urlrewrites", there is a class calledThundermain. 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 shocould be implemented, and in our code that is very simple. It merely stores the urlrewrites element for later use:

<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> Object </span> Create (<SPAN class = "CS-keyword ""> Object </span> parent, <SPAN class = "CS-keyword"> Object </span> configcontext, xmlnode section) {_ orules = Section; <SPAN class = "CS-keyword"> return </span> <SPAN class = "CS-keyword"> This </span> ;}
Initiating the rewrite Process

Coming back to actually rewriting the URL, as I said earlier, we need to do something inApplication_beginrequest ()Event in global. asax-we just delegate this to another class:

<SPAN class = "CS-keyword"> protected </span> <SPAN class = "CS-keyword"> void </span> application_beginrequest (Object sender, eventargs E) {thundermain. urlrewriter. rewriter. process ();}

Which callthe 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 workGetsubstitution ()-An instance method of this class.

<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> static </span> <SPAN class = "CS-keyword"> void </span> process () {rewriter orewriter = (rewriter) configurationsettings. getconfig ("system. web/urlrewrites "); <SPAN class =" CS-keyword "> string </span> zsubst = orewriter. getsubstitution (httpcontext. current. request. path); <SPAN class = "CS-keyword"> If </span> (zsubst. length & gt; <SPAN class = "CS-literal"> 0 </span>) {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:

<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> string </span> getsubstitution (<SPAN class = "CS-Keyword "> string </span> zpath) {RegEx oreg; <SPAN class = "CS-keyword"> foreach </span> (xmlnode onode <SPAN class = "CS-keyword"> in </span> _ orules. selectnodes ("rule") {oreg = <SPAN class = "CS-keyword"> New </span> RegEx (onode. selectsinglenode ("url/text ()"). value); match omatch = oreg. match (zpath); <SPAN class = "CS-keyword"> If </span> (omatch. success) {<SPAN class = "CS-keyword"> return </span> oreg. replace (zpath, onode. selectsinglenode ("rewrite/text ()"). value) ;}< SPAN class = "CS-keyword"> return </span> 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...

Finally

There's one major caveat with all this. if you want to process a request with a file extension other. aspx such. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.