In this case, if the user accesses/2004/02/14. aspx, We need to rewrite the URL to showblogcontent. aspx? Year = 2004 & month = 2 & day = 14. All three cases (the URL specifies the year, month, and day; the URL only specifies the year and month; the URL only specifies the year) can be processed using the rewrite rule:
<Rewriterconfig>
<Rules>
<! -- Blog content displayProgramRule -->
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/(\ D {2})/(\ D {2}) \. aspx </lookfor>
<Sendto> ~ /Showblogcontent. aspx? Year = $1 & month = $2 & day = $3 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/(\ D {2})/Default \. aspx </lookfor>
<Sendto> <! [CDATA [~ /Showblogcontent. aspx? Year = $1 & month = $2]> </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/Default \. aspx </lookfor>
<Sendto> ~ /Showblogcontent. aspx? Year = $1 </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>
These Rewrite Rules indicate the function of the regular expression. In the first rule, we use the mode (\ D {4})/(\ D {2})/(\ D {2}) \. aspx to find the URL. In concise English, it corresponds to a string of four numbers, followed by a slash, followed by two numbers, followed by a slash, followed by two numbers, and finally one. aspx. Brackets around each number Group are very important. You can use them to reference matching characters in the corresponding <sendto> attribute. In particular, we can use $1, $2, and $3 to reference the matching groups in the parentheses for the first, second, and third parentheses respectively.
Note: Because the Web. config file uses XML format, the characters (such as &, <, and>) in the element text must be escaped. In the <sendto> element of the first rule, & is escaped &. Another technology is used in <sendto> of the second rule (use <! [CDATA [...]> element), without escaping internal content. You can use either of the two methods and get the same result.
Figure 5, 6, and 7 show URL rewriting in the action. The data is actually dragged from my blog http://scottonwriting.net. Figure 5 shows the November 7, 2003 posts; Figure 6 shows all posts in November 2003; Figure 7 shows all posts in 2003.
Figure 5. Post in November 7, 2003
Figure 6. All posts in November 2003
Figure 7. All posts in 2003
Note: The URL rewriting engine must use the regular expression mode in the <lookfor> element. If you are not familiar with regular expressions, read an article I wrote earlier.ArticleAn Introduction to regular expressions. In addition, there is also a good website: regexlib.com, where you can get help information about common Regular Expressions and share your own custom regular expressions.
Build essential directory structures
When the request is/2004/03/19. aspx, IIS will notify the. aspx extension and route the request to the ASP. NET engine. When a request is moved in an ASP. NET Engine Pipeline, the URL will be rewritten to showblogcontent. aspx? Year = 2004 & month = 03 & day = 19, and visitors will see the March 19, 2004 blog entry. But what happens when the user browses/2004/03? IIS returns a 2004/03 error unless there is a/404/directory. In addition, this directory also requires a default. aspx page to pass requests to the ASP. NET engine.
Therefore, to use this method, you must manually create a directory (including blog entries) for each year, and the directory contains a default. aspx page. In addition, you need to manually create 12 directories (01, 02 ,? ,?... , 12), and each directory contains a default. aspx file. (As described above, you must add a default. aspx file to the/products/directory to display listcategories. aspx correctly when accessing/products .)
Obviously, adding such a directory structure may be very painful. To solve this problem, all incoming IIS requests are mapped to the ASP. NET engine. In this way, IIS will pass requests to the ASP. NET engine (even if the/2004/03/directory does not exist) even if the URL/2004/03/is accessed ). However, using this method will make ASP. the net engine processes all types of incoming requests to the Web server, including images, CSS files, external & #106 avascript files, Macromedia Flash files, and so on.
The comprehensive discussion of processing all file types is far beyond the scope of this article. For examples of ASP. NET web applications that use this technology, see. text, an open source blog engine .. Text can be configured to map all requests to the ASP. NET engine. It can use a custom HTTP handler to process the issue of generating all file types. Custom HTTP handlers learn how to generate typical static file types (images, CSS files, and so on ).
Back to Top
Conclusion
In this article, we discuss how to use the rewriteurl () method of the httpcontext class to execute URL rewriting at the ASP. NET level. As we can see, rewriteurl () updates the specific httpcontext's request attribute, thus updating the requested file and path. The final result is that, from the user's perspective, they want to access a specific URL, but from the Web server side, the requested URL is another.
You can rewrite the URL in the HTTP module or HTTP processing program. In this article, we will introduce how to use the HTTP module to execute rewriting and discuss the results of executing rewriting at different stages in the pipeline.
of course, if ASP. NET-level rewriting is executed, URL rewriting will only occur if the request is successfully passed from IIS to the ASP. NET engine. In fact, this happens only when the user requests a page with the. aspx extension. However, if you want to enable the user to access a URL that does not actually exist, but want to rewrite it to the existing ASP.. NET page, you must create a virtual directory and default. ASPX page, or configure IIS to route all incoming requests to ASP.. net engine.