[Transfer] Asp.net rewrite urlrewriter with URL to implement any second-level domain name

Source: Internet
Author: User

Come hereArticleI should know what a second-level domain name is, so I won't talk nonsense. 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.kerry.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, http://love.kerry.com then we changed this address toProgramRequired address with parameters, http://kerry.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.kerry.com ";
// Process the original URL and get the final required URL. The value is http://kerry.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. for the last two questions, use the specificCodeDemonstrate how to rewrite the target URL to meet our requirements

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 * .kerry.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 ing aims to ASP. net ISAPI takes over any second-level domain name site that is not specified 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 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.kerry.com is kerry.com/action.aspx? Id = 1
The actual processing page of call.kerryl.com is kerry.com/action.aspx? Id = 2
The actual processing page of unzip with.kerry.com is kerry.com/developer.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.kerry.com/developer.aspx

Http: // localhost/Kerry/action. aspx? Id = 14 like.kerry.com/developer.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 );

I found another solution on the internet...

Maybe you mean this 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.