The principle of the ASP. NET-level URL rewrite component is very simple. In fact, it only listens to the beginrequest event and determines the target URL according to the configuration. In my previous projects, I found that urlrewriter is frequently used as the URL rewrite component. I think it may be because it is something provided by Microsoft.
To use urlrewriter, configure an httpmodule in Web. config:
< Httpmodules >
< Add Name = " Modulerewriter"
Type = " Urlrewriter. modulerewriter, urlrewriter"/>
</ Httpmodules >
Then the configuration is completed. (Note: we strongly recommend that you use the configpath attribute to extract the configuration into additional files for ease of management ):
< Configsections >
< Section Name = " Rewriterconfig"
Type = " Urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</ Configsections >
< Rewriterconfig >
< Rules >
< Rewriterrule >
< Lookfor > ~ /Tag/([\ W] + )/ </Lookfor>
< Sendto > ~ /Tags. aspx? Tag = $1 </Sendto>
</ Rewriterrule >
</ Rules >
</ Rewriterconfig >
A regular expression is a very bad thing. It can be matched and captured. In the preceding example, we locate/Tag/xxx that meets the lookfor condition to tags. on the ASPX page, XXX is used as the value of the tag querystring item.CodeHttpcontext. Request. querystring ["tag"] to obtain the value.
The urlrewriter feature is sufficient for most applications, but I always don't like it. But if I have to ask why I don't like it, it's hard for me to say it's ugly. This configuration method may be the only problem. When URL rewriter is used, the configuration segment is often very long. Each configuration item requires four lines of code from <rewriterrule> to </rewriterrule>, for a small project, hundreds of rows of configuration are easily displayed. "This is too XML." I think, why not use XML Attribute? In this way, each configuration item can be shortened to one line.
Therefore, if I want to do URL rewrite, I usually use the open source component urlrewriter. Net produced by intelligencia. Although this name is very similar to the previous one, it has far more functions than the former. This component is similar to urlrewriter in use (in fact, it seems that all URL rewrite components are similar). All we need to do is configure:
< Configsections >
< Section Name = " Rewriter"
Type = " Intelligencia. urlrewriter. configuration. Rewriterconfigurationsectionhandler,
Intelligencia. urlrewriter " />
</ Configsections >
< Rewriter >
< Rewrite URL = " ^/User/(\ D +) $" To="~ /User. aspx? Id = $1" Processing="Stop"/>
< Rewrite URL = "^/User/(\ W +) $" To="~ /User. aspx? Name = $1" Processing="Stop"/>
</ Rewriter >
< System. Web >
< Httpmodules >
< Add Name = " Urlrewriter"
Type = " Intelligencia. urlrewriter. rewriterhttpmodule,
Intelligencia. urlrewriter " />
</ Httpmodules >
</ System. Web >
We mainly look at the configuration item of the rewrite rule <rewriter/>. Unlike urlrewriter, urlrewriter. net uses each of my favorite rules as a node, which simplifies the rewrite rules for the entire project. But what does processing = "stop" mean? This will talk about urlrewriter. Net's method for processing rewrite rules. When urlrewriter. Net finds a matching rewrite rule, it will not stop, but will continue to look for other matching items. The final effect is that it can match the last rewrite rule of the current request. If we need urlrewriter. Net to take effect after a match is found, we need to set the processing attribute to stop. For example, in the preceding configuration, if "/user/" is followed by a number, the user ID is used for search. Otherwise, the user name is used.
If urlrewriter. NET is simple in configuration, it has no advantages over urlrewriter. However, the capabilities of urlrewriter. NET are far more powerful than that. What we just used is actually one of the actions it provides. For the first time, it also provides many actions:
- If
- Unless
- Rewrite
- Redirect
- Setstatus
- Forbidden
- Gone
- Not-Allowed
- Not-found
- Not-implemented
- Addheader
- Setcookie
- Setproperty
Action alone is not enough. urlrewriter. Net also provides condition, transform, default document, parser, error handler, logger, and other functions, and can "Express" complex logic through expression. This is still the configuration. It's just programming! Yes, you can use urlrewriter. Net to express the request-reply logic through configuration, which undoubtedly brings us great convenience. Here, I cannot elaborate on all aspects of urlrewriter. net. If you are interested, you can refer to the reference provided on its official website.
"I have to say this to components, but I still want to recommend another component here. In some special cases, urlrewriter. Net cannot meet our requirements. Hmm? Can it be expanded by itself? That's right, but -- sell a token first. This issue is described in the last part of this series. Urlrewriter. NET provides URL rewriter at the ASP. NET level. If you want to perform URL rewrite at the IIS level, you must use other methods. ISAPI rewrite is a famous component for URL rewrite at the IIS level. Unfortunately, this is a commercial component and we need to buy it with a US knife. So here I recommend another open-source product: iirf (ionic's ISAPI rewrite filter ).
Because URL rewrite is performed on IIS, The iirf configuration method is different from urlrewriter. net. If you want to use iirf, you need to add isapirewrite4.dll to the ISAPI filter list of the web site:
Iirf is configured through the INI file. Put isapirewrite4.ini and isapirewrite4.dll in the same directory:
rewriterule ^/user/(\ D +) $/user. aspx? Id = $1 [I, L]
rewriterule ^/user/(\ W +) $/user. aspx? Name = $1 [I, L]
The iirf rewrite rule is "rewriterule <URL-pattern> <destination> [<modifiers>]". There is no limit on the number of spaces between each part, but it must be a space, it cannot be other blank characters such as tab. The key to "url-pattern" and "destination" is modifier. There are many modifiers in iirf. Here I will only introduce the two mentioned above. "I" indicates that uppercase and lowercase letters are irrelevant to the matching. "l" is used as the urlrewriter. similar to processing = "stop" in.
Although iirf is an open-source component, it is still powerful. Especially after combining rewritecond (rewrite condition), you can implement complicated rewrite rules. For example, the following configuration overwrites the root directory request of useragent containing googlebot to a specific resource:
Rewritecond % {http_user_agent} ^ googlebot .*
Rewriterule ^/$/googlebot.html [l]
Finally, let's take a look at the difference between the two rewrite components. Obviously, the biggest difference is that they are rewritten at different levels, the following two pictures describe how they rewrite the "/user/jeffz" request that originally expected "404 resource not found" result to "/user/ name = jeffz.
First, urlrewriter. Net URL rewrite on the ASP. NET layer:
Next, the iirf URL rewrite at the IIS level:
With these two components, we believe that we no longer need anything else to implement URL rewrite.
Related links:
(1) IIS and ASP. NET
(3) keep the PostBack address after URL rewrite
(4) details and features of different levels of URL rewrite
------ Source: Lao Zhao;