Asp.net 2.0 rewrite urlrewriter through URL to implement any second-level domain name

Source: Internet
Author: User
Asp.net 2.0 rewrite urlrewriter through URL to implement any second-level domain name

I should know what a second-level domain name is. I will not talk nonsense about it. But before the discussion, I should first understand an ideological problem.
The problem that many of my friends have been confused (I have been confused a few days ago) is that after I type an address, how can this URL be rewritten?
Step 1: What happened when I typed an address in a browser, such as a http://love.yourdomain.com, and clicked enter?
To simplify the problem, I will explain it as follows:
Step 2: first, the entered address is parsed and finally comes to a Web server. to IIS for processing. in. in the net world, IIS will send such requests to a web processor for processing. Finally, the Web processor returns the processing results to the browser and displays them to users.
Please do not ignore this problem. Everything in step 2 is done on the server side. during these operations, the address on the browser of the user side will not change. even if the web processor returns the processing result, the above address will not change.
The URL entered at the beginning serves only as a knock-on interface. After the door is knocked out, the function is completed. Only your eyes can see the address, the browser, the server does not know the address.
The problem that we need to understand is that URL rewriting is only an insider situation that web developers know, and users do not know what happened, he thinks that the address he typed is the result displayed on the screen. that is to say, we control the content to be displayed behind the scenes.
Next, we should consider how to control the displayed content?
From the process mentioned above, it is obvious that you need to work in the Web processor step.

One of the simplest considerations is that the user typed in a simple address without any parameters, the http://love.yourdomain.com then we changed this address to an address that fits the needs of the program with parameters, http://yourdomain.com? Lover = notus.
This step is called URL rewriting.
In. Net terminology, We need to register an httpmodule for the application to process specific URLs.
Register httpmodule, in Web. config,
Processing URL, In the httpmodule program we provide

It is basically equivalent to a program like this.

// Use our httpmodule program to capture the original URL
String originalurl = "http://love.yourdomain.com ";
// Process the original URL and get the final required URL. The value is http://yourdomain.com? Lover = notus
String finalurl = rewrite (originalurl );
// Context re-sends the URL internally to IIS for processing
Context. rewritepath (finalurl );

Next, we will rewrite the URL.
Step 1: Determine the URLs to be overwritten, that is, the rewrite rules.
Step 2: Compile the httpmodule Handler
Step 3: Integrate the compiled httpmodule into the web program and start working.

The above is the basic knowledge of URL rewriting, and the process of using URL rewriting to implement second-level domain names is the same. because both the second-level domain name and third-level domain name are URLs. as long as we intercept this URL address, we can start to process it.

These jobs are troublesome, but some people on the Internet have written such a program for us. refer to 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.

Some problems may occur during the implementation process, mostly because the above articles are not carefully produced, but to be honest, it is not easy to read long articles. below I will record some important issues. the last two problems show how to rewrite the target URL with specific code to meet our requirements.

Why do I have to use wildcard parsing?
After reading replies from many friends, I think there may be such a misunderstanding: This article on URL rewriting only introduces some processing methods. it is not important that pan resolution is not pan resolution.
If you do not need to implement any second-level domain name, you do not need to implement Pan resolution. You can directly fix the second-level domain name you need, and then process it in URL rewriting!
Further, if you do not need to implement a second-level domain name, but simply rewrite the URL under a fixed domain name, you do not need to modify the urlrewriter of msdn, simple URL rewriting can be implemented directly. zyw only makes modifications to this project to get all the URLs for greater control. as we can see, the urlrewriter at the beginning of msdn does not care about Domain Name issues.
I wrote this article at the beginning because I recently used it in my project.

What is Microsoft's urlrewriter? Where can I download this project?
This is the sample program provided in the previous article about urlrewriter on msdn. You can download it here
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

How to use the code? Trouble?
Certainly, it is not troublesome. The following things are to be done:
Download the code to your machine.
After installation, add the urlrewriter project to your project.
Modify the code according to the method in the address given above
Configure web. config to start using.

What is httpmodule?

Simply put, it is a program that processes HTTP requests.
For more information, see the SDK documentation.

How to Implement Pan resolution?

First, add a second-level domain name * .yourdomain.com to the Domain Name Service Provider, pointing to your server IP address.
Then, create a site in IIS. The Host Header of this site is left blank, and the port is usually 80. This site is the default website of port 80 on the server.
Add a wildcard application ing (IIS Site Properties> main directory> Configuration) to the site ), the purpose of this ing is to require Asp.net ISAPI to take over any second-level domain site that is not explicitly defined in IIS.

What happens when I enter a second-level domain name?
When IIS detects that the input URL is a second-level domain name, it first checks whether there is a site registered with this second-level domain name on IIS. If yes, It is transferred to this site. Otherwise, the default site is the site with the Host Header empty. therefore, a port can only have a site with an empty Host header.
We have set up Asp.net ISAPI to take over these children who do not have a home. Write the program, analyze the passed URL, and rewrite the program.

Why does my httpmodule seem to be ineffective?

After a breakpoint is set in the httpmodule program, the process does not go from here in any case. the reason is that you did not register your httpmodule program with the web program. this job needs to be performed on the web. config.
<System. Web>
<Httpmodules>
<Add type = "urlrewriter. modulerewriter, urlrewriter" name = "modulerewriter"/>
</Httpmodules>
</System. Web>

Why do I always prompt "unknown configuration section rewriterconfig error"

This is because you have not registered your rewriterconfig configuration section with the web program. This work needs to be completed in Web. config.
<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>
Then, you can use the rewriterconfig section in <configuration> to configure rules.

Which part of the HTTP module processes the URL?

Most of the work is in the urlrewriter. modulerewriter. Rewrite () method. The key phase is here:
If (Re. ismatch (requestedpath ))
Obviously, we will check whether the input URL is the URL we want to rewrite,
String sendtourl = rewriterutils. resolveurl (App. Context. Request. applicationpath, re. Replace (requestedpath, rules [I]. sendto ));
Here, the target URL configured in Web. config is accepted.
Rewriterutils. rewriteurl (App. Context, sendtourl );
Rewrite the URL internally.

I don't want to write the second-level domain name to Web. config, and the target url I want to rewrite cannot be written to. For example, we have such a need.
The actual processing page of love.yourdomain.com is yourdomain.com/action.aspx? Id = 1
The actual processing page of call.kerryl.com is yourdomain.com/action.aspx? Id = 2
The actual processing page of walkwith.yourdomain.com is yourdomain.com/pai.aspx.
What should I do?

At this time, you need to do something in the Code mentioned above.
If (Re. ismatch (requestedpath ))
{

// Find the second-level domain name in the URL
String [] Userhost = app. Request. url. Host. Split (New char [] {'.'});
String domain2 = Userhost [0];

// Set the target URL to be rewritten 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 = "starts ")
Sendtourl = "/walk. aspx ";

Rewriterutils. rewriteurl (App. Context, sendtourl );

}

This is required when you configure rules in Web. config.
<Rewriterrule>
<Lookfor> http: // (/W +)/. Kerry/. com </lookfor>
<Sendto>/test. aspx </sendto>
</Rewriterrule>
(/W +) used to match any string
Test. aspx can be written at will, because we didn't use it at all.

I have a lot of sites that are not sure about the second-level domain name, but the page of each site is determined that the content of each second-level domain name site is actually tuned from the database by different IDs,
This is the case.
Http: // localhost/Kerry/action. aspx? Id = 1 love.yourdomain.com/#.aspx

Http: // localhost/Kerry/action. aspx? Id = 14 like.yourdomain.com/#.aspx

If the ID parameter cannot be displayed, it is changed to a second-level domain name. 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 it like this in the program.
// Obtain the second-level domain name
String [] Userhost = app. Request. url. Host. Split (New char [] {'.'});
String domain2 = Userhost [0];
Obtain different numbers based on the domain name
Int id = getidfromdomain (domain2 );
// Obtain the basic URL to be switched
String sendtourl = rewriterutils. resolveurl (App. Context. Request. applicationpath, re. Replace (requestedpath, rules [I]. sendto ));
// Add the ID Parameter
If (ID> 0)
Sendtourl = string. Format ("{0 }? Id = {1} ", sendtourl, ID );
Else
Sendtourl = "error. aspx ";
// Rewrite
Rewriterutils. rewriteurl (App. Context, sendtourl );

How to match directories? I wrote a lookfor rule http://love.yourdomain.com/, but the address entered by the browser cannot be overwritten correctly. After Trace, I found that there is no matching at all. Why?

First, we should know that the browser does not actually accept http://love.yourdomain.com/, but http://love.yourdomain.com/default.aspx. Therefore, your </lookfor> rules should be written as follows:
<Lookfor>
Http://love.yourdomain.com/default.aspx
</Lookfor>
This default. aspx should be the default document you configured in IIS. If you have index. aspx or other strange names, write them as your own name.
Similarly, the http://love.yourdomain.com/fun address needs to match and such a rule http://love.yourdomain.com/fun/default.aspx
However, to put it bluntly, you do not need to have the fun directory in your file, because it is... rewritten.


I found another solution on the internet...

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. the reason why I didn't put this column at the beginning is that this practice is a bit clever and may not be so easy to understand at the beginning. however, I believe that the last friend will smile at this article.

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.