asp.net use URL rewrite urlrewriter to implement any level two domain name Advanced article _ Practical skills

Source: Internet
Author: User
Tags server port
I recently wrote a small example, you can see this first, there are small examples of the complete code download
Http://www.jb51.net/article/20906.htm

For a long time did not write technical articles, if you do not understand, see more than a few, Khan, or, in the back of the article (this is the most effective way), I will try to help everyone to answer questions.

To find this article, should know what is called the level two domain name, nonsense will not say. But before we talk, we need to understand a question of thought.
A lot of friends have been thinking about (I have been confused the last few days) The question is, I typed an address, how the URL has been rewritten?
The first step: in the browser type an address, such as http://love.kerry.com, after the point of return, what happened?
To simplify the problem, let me explain this:
Step two: First, the address that you typed is parsed, and eventually comes to a Web server. To IIS. In the. NET world, IIS will send such a request to a web processor, and finally, the Web processor returns the results of the processing to the browser, which is displayed to the user.
You don't have to ignore this, and everything in the second step is done on the server side. When these things go on, the address on the client's browser does not change. Even if the last Web processor returns the processing results, the address above will not change.
At the beginning of the URL to type, just a knock on the role of the door, the effect even if the end, only your eyes can see that address, browser, server and so do not know this address.
The problem, then, is that the so-called URL rewrite is just the insider information the web Developer knows, and the user has no idea what's going on, and he thinks he's typing the address that should come out of the screen. In other words, we control what we want to display behind the scenes.
The next thing to consider is, how to control the display of content?
From the above process, it is clear that the work of the Web processor is a step in the foot.

One of the simplest considerations is that the user has typed a simple address with no parameters, http://love.kerry.com and then we change this address to a program-required address with parameters, http://kerry.com? Lover=notus, and finally deal with it.
The so-called URL rewrite is in this step.
In the. NET terminology, we need to register a httpmodule for the application to handle a particular URL
Registered HttpModule, in web.config,
Processing URLs, in the HttpModule program we provide

Roughly equivalent to a procedure like this

Intercept the original URL with our HttpModule program
String originalurl= " http://love.kerry.com";
Process the original URL, get the last required URL, the value is http://kerry.com?lover=notus
String Finalurl=rewrite (Originalurl);
The context sends the URL internally to IIS for processing
Context. RewritePath (FinalURL);

Next, we'll implement URL rewriting.
Step one: Determine which URLs you want to override, that is, make the rewrite rules
Step two: Write the HttpModule handler
Step three: Integrate the written httpmodule into the Web program and start working.

The above is the basic knowledge of URL rewriting, and using URL rewrite to achieve level two domain name, the process is the same. Because whether it is a level two domain name or a level three domain name, is a URL address. As long as we intercept this URL, we can do it at the time of processing.

These jobs are very troublesome, but the internet has a high man to write us this program, see the following article:

Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

Http://www.cnblogs.com/jzywh/archive/2005/09/29/246650.html

Http://www.cnblogs.com/jzywh/archive/2006/02/20/334004.html

The article is over.

There will be some problems in the implementation process, mostly because look at the above article is not carefully produced, but to tell the truth, so long articles to read it is not easy. Let me write down some important questions. The last two questions, with specific code showing how to handle the overridden target URL to meet our requirements

Why do I have to use a pan-parsing?
Read a lot of friends reply, I think now there may be such a misunderstanding, that is, this article on the URL rewrite is just to introduce some of the processing methods. It doesn't matter if the pan-resolution doesn't pan-parse.
If you do not need to implement any two-level domain name, it is not necessary to implement the pan-resolution, directly to the level of the two domain name you need to die, and then in the URL rewrite to deal with it!
Step back , if even the two-level domain name is not implemented , just a fixed domain name under the URL to rewrite , that does not need to modify the MSDN Urlrewriter, you can use it directly to achieve a simple URL rewrite. Zyw's changes to the project are only for greater control over the entire URL. And as we've seen, the first MSDN urlrewriter doesn't care about domain names.
I started with this topic because I used it recently in my project and wrote it when I was writing the document .



What is the urlrewriter of Microsoft and where is this project downloaded?
This is the sample program that is available in the MSDN previous article on Urlrewriter, where you can download
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx


How do you use this code?
Sure, no trouble, there are things to be done:
Download the code to your machine.
After installation, add the Urlrewriter project to your own project
Modify the code according to the method in the address given above
Configure Web.config to start using.


What is HttpModule?

A simple understanding is a program that handles HTTP requests
For more detailed understanding, consult the SDK documentation.


How do I implement a pan-resolution?

First, add a *.kerry.com level two domain name to your server IP at the domain Name Service provider.
Then, set up a site in IIS, the main header of this site is left blank, the general port is 80. This site is the default Web site for the entire server port 80.
Add a wildcard application map to this site (the IIS site properties-> The home directory-> configuration), which is intended to asp.net ISAPI to take over any two-level domain name sites that are not explicitly in IIS.


Random input Level Two domain name, what happened?
When IIS detects that the incoming URL is a level two domain name, it will first check the IIS has not registered this two-level domain name site, if there is, go to this site, otherwise, will go to the default site, the default site is the previous configuration of the main header for the empty site. So, A port can have only one site with an empty host header.
We have set up the ASP.net ISAPI to take over the children without home. Write the program, parse the incoming URL, and perform the rewrite.


Why doesn't my httpmodule seem to work?

After setting breakpoints in the HttpModule program, no matter what, the process does not go from here. The reason is that you did not register your HttpModule program with the Web application. This job needs to be done in web.config.
<system.web>
<add type= "Urlrewriter.modulerewriter, Urlrewriter" name= "Modulerewriter"/>
</system.web>


Why am I always prompted for "Unknown configuration section rewriterconfig error"

This is because you did not register your rewriterconfig configuration section with the Web application. This work needs to be done in web.config.
<configSections>
<section name= "Rewriterconfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/ >
</configSections>
Then, you can use the Rewriterconfig section to configure the rules in <configuration>.


What part of the HttpModule does the URL handle?

Most of the work is in Urlrewriter. Modulerewriter. Rewrite () method. The key phase is here:
if (re. IsMatch (Requestedpath))
Obviously, the incoming URL is the URL we want to rewrite, so let's see,
String Sendtourl = Rewriterutils.resolveurl (app. Context.Request.ApplicationPath, Re. Replace (Requestedpath, Rules[i]. SendTo));
This accepts the target URL that is configured in Web.config to go to
Rewriterutils.rewriteurl (app. context, Sendtourl);
Rewrite the URL internally.


I do not want to write the two domain name dead in the web.config, and I will rewrite the target URL can not be written dead. Like we have this need
Love.kerry.com The actual processing page is kerry.com/action.aspx?id=1
Call.kerryl.com The actual processing page is kerry.com/action.aspx?id=2
Walkwith.kerry.com The actual processing page is kerry.com/walk.aspx
How do you deal with it?


At this time, you need to be in the above mentioned code to tamper with.
if (re. IsMatch (Requestedpath))
{

Find the two-level domain name in the URL
string [] Userhost = App. Request.Url.Host.Split (New Char [] {'. '});
String domain2=userhost [0];

Set the target URL to override as needed
String Sendtourl;
if (domain2== "Love")
Sendtourl = "/action.aspx?id=1";
else if (domain2== "call")
Sendtourl = "/action.aspx?id=2";
else I f (domain2== "Walkwith")
Sendtourl = "/walk.aspx";

Rewriterutils.rewriteurl (app. context, Sendtourl);

}

When you configure the rules in Web.config, you need this.
<RewriterRule>
<lookfor>http://(\w+) \.kerry\.com</lookfor>
<SendTo>/test.aspx</SendTo>
</RewriterRule>
(\w+) used to match any string
The test.aspx here can write anything else, because we don't use it at all.


I have a lot of uncertain level two domain name site, but each site's page is determined, each level two domain name site content actually root play different ID from the database tune,
Things like this
http://localhost/kerry/action.aspx?id=1 love.kerry.com/walk.aspx

http://localhost/kerry/action.aspx?id=14 like.kerry.com/walk.aspx

Now go up, can not display ID parameters, are changed to the two-level domain name way. What should I do at this time?

Configure rules first
<rewriterrule>
<lookfor>http://(\w+) \.kerry \.com\ walk.aspx</lookfor>
< Sendto>/action.aspx</sendto>
</rewriterrule>
and then process the
//Get level Two domain name
string [] userhost in the program App. Request.Url.Host.Split (New Char [] {'. '});
string domain2=userhost [0];
Get a different number according to the domain name
int id=getidfromdomain (domain2);
Get the basic URL to turn to
 string Sendtourl = Rewriterutils.resolveurl (app. Context.Request.ApplicationPath, Re. Replace (Requestedpath, Rules[i]. SendTo)); The
//plus ID parameter
if (id>0)
Sendtourl=string. Format ("{0}?id={1}", Sendtourl, id);
Else
    sendtourl= "error.aspx";
Overrides the
Rewriterutils.rewriteurl (app. context, Sendtourl); How does


match a directory? Write a lookfor rule http://love.kerry.com/, but in the browser input this address, always can not correct rewrite, After trace, we find that there is no match at all, why?

First of all, we should know that browsers actually accept not http://love.kerry.com/, but http://love.kerry.com/default.aspx , so your </ Lookfor> rules should be written like this
<LookFor>
Http://love.kerry.com/default.aspx
</LookFor>
This default.aspx should be the default document you configure in IIS, and if you have index.aspx or other strange names, write your own name.
Similarly, Http://love.kerry.com/fun this address to match, the need for such a rule http://love.kerry.com/fun/default.aspx
But, again, your document does not need to have fun this directory, because ... Rewrite it.


I searched the internet there is another way to solve ...

Maybe you mean this article.

Http://blog.csdn.net/mengyao/archive/2007/01/25/1493537.aspx

As you can see, the basic methods are the same. This is not the first, because this approach is a bit tricky, may not be so good at the beginning of understanding. But I believe that the last friend to see this article, should be a smile
Happy programming


Finally PS: Originally prepared for a few days to send this article, just now, accidentally from VSS to write these days of the program are deleted, or thoroughly, again strongly despise Microsoft do this garbage source manager-_-

Related Article

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.