Revisit URL Rewrite (4): Some details and characteristics of different levels of URL Rewrite

Source: Internet
Author: User
Tags httpcontext iis

In previous articles we have talked about several key aspects of URL rewrite. In the last article in this series, we'll discuss some of the details and characteristics of different levels of URL rewrite.

In theory, the IIS-level URL rewrite is written in C or C + +, and is more performance-rewrite than asp.net-level URLs written using managed code. But I think the gap is negligible in most cases, and this performance is almost impossible to become a performance bottleneck. So choosing which level of URL rewrite is typically not determined by your application's performance requirements. So what kind of URL rewrite should you use? After using different levels of URL rewrite, what should we pay attention to? I'm here to talk about my personal opinion.

Requirements for URL rewrite functionality

Although the current URL rewrite component is functionally capable of satisfying most applications, at some point we do need some special features. For example, the URL Rewrite based on the domain name, for the current URL Rewrite component, want to implement this is not easy. The commercial ISAPI rewrite can now support this, but the urlrewriter.net and IIRF of open source have not been able to function in this area. They are all matched according to the path of the request relative to the site, and the domain name that is requested cannot be used as a matching criterion. This requires that we extend the URL rewrite component. For most. NET developers, managed code is naturally the preferred development, and you may want to choose ASP.net-level URL rewrite rewrite components. However, there are many examples of extensions available online, whether it's asp.net level urlrewriter.net or IIS-level IIRF.

But in fact, if we want to achieve the above function, we can do it in two separate steps. First we use IIRF for URL Rewrite at the IIS level, then we make further URL Rewrite at the asp.net level. For example, we now want to implement the "Http://jeffz.domain.com/articles" rewrite to "/articlelist.aspx?owner=jeffz", you can first in the IIRF to do the first URL Rewrite, the purpose is to "/ Articles "rewrite to/articlelist.aspx."

Rewriterule ^/articles$/articlelist.aspx [I, L, U]

In this way, ASP. NET engine directly receives a request for/articlelist.aspx. And then inside the asp.net, we can make a second URL Rewrite (for convenience, I'm still writing here in Global.asax, and it's recommended to use an extra httpmodule in the project).

protected void Application_BeginRequest (object sender, EventArgs e)
{
HttpContext context =  HttpContext.Current;
String host = context.  Request.Url.Host;
String owner = host. Substring (0, host.  IndexOf ('. '));
Context. RewritePath (context. Request.rawurl + "? owner=" + owner);
}

After two URLs Rewrite, we have achieved the desired effect (in the actual project, the above code can not be used directly, because you need to determine whether there are query string, etc.).

In addition, ASP. Net-level URL rewrite can only work in asp.net (obviously), and if you want the URL rewrite to support other server technologies such as Php,ror, you can only use IIS-level URLs rewrite (or other server technology-provided URLs Rewrite function).

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.