Asp.net rewrite urlrewriter with URL to display any level 2 Domain Name

Source: Internet
Author: User

I should understand what a second-level domain name is when I come to this text file. I will not talk about this nonsense. But before the discussion, I should first understand a difficult idea.
Many of my friends have been unable to figure out how to rewrite the URL after I type an address?
Step 1: Enter an address in the browser, such as http://love.kerry.com, after clicking enter, all except what?
To simplify the difficulties, let me explain the following:
Step 2: first, the entered address is parsed and finally comes to a Web server. to IIS for processing. in. in the world of. net, 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.
Do not ignore such a difficulty. All the events in step 2 are done on the server. when these events are stopped, the address on the browser of the client will not change. the address above will not change even if the web processor returns the processing result.
The URL entered at the beginning is just a knock-on function. After the knock-on function is completed, the function is completed. Only your eyes can see the address, the browser, the server does not understand the address.
The difficulty is that the so-called URL rewriting is only the inside story of web development, and the user does not understand anything, he thought the address he typed should be displayed on the screen. that is to say, we control the content to be displayed behind the scenes.
Next, we will plan how to control the displayed content?
From the process mentioned above, it is obvious that you need to work in the Web processor.

One of the simplest planning is that the user typed a simple address without any parameters, http://love.kerry.com and then we change that address to a address with parameters that meet the needs of the program, http://kerry.com? Lover = us.
The so-called URL rewriting is in this step.
In. Net terminology, We need to register an httpmodule for the actual application to process specific URLs.
Register httpmodule, in Web. config,
Process the URL. In the httpmodule program we help

It is basically equivalent to a program like that.

// Use our httpmodule program to capture the original URL
String originalurl = "http://love.kerry.com ";
// Process the original URL and get the URL of the final requirement. Is the value http://kerry.com? Lover = us
String finalurl = rerecord (originalurl );
// Context re-sends the URL internally to IIS for processing
Context. rerecordpath (finalurl );

Next, we will see URL rewriting.
Step 1: Determine the URLs to be rewritten, 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 display Level 2 domain names is the same. because both level 2 domain names and Level 3 domain names are URLs. as long as we intercept the URL address, there will be no score in the processing time.

These jobs are quite troublesome, but some people on the Internet have already written such a program for us. For more information, see the following text:

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

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

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

The text file is over.

Some difficulties will be encountered during the implementation process, most of which are due to the fact that the above text works are not carefully produced, but to be honest, it is not easy to finish reading long text works. below I will record some important difficulties. the last two difficulties show how to rewrite the target URL with the specific source code to meet our requirements.

What is Microsoft's urlrerecordr? Where did the project be downloaded?
This is the sample program used in the previous article on msdn to introduce urlrerecordr.
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

How to apply the source code? Trouble?
Certainly, it is not troublesome. The following events are required:
Download the source code to your machine.
After installation, add the urlrerecordr project to your project.
Follow the tips in the above address to correct the source code
Configure web. config to start application.

What is httpmodule?

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

How does one see Pan resolution?

First, add a Level 2 domain name * .kerry.com to the Domain Name Service Provider, pointing to your server IP address.
Then, create a site in IIS. Leave the Host header of the site blank. The port is usually 80. The site is the default Internet of port 80 on the server.
Add a wildcard program ing to the site (IIS Site properties-> master list-> Configuration ), the purpose of the ing is to require Asp.net ISAPI to take over any level 2 domain name site that is not explicitly defined in IIS.

What is the time for entering a second-level domain name?
When IIS checks and tests the time when the input URL is a Level 2 domain name, it first checks whether there is a site with the level 2 domain name registered on IIS. If yes, transfer to the site. Otherwise, the site will be transferred to the default site, which is the site with the previously configured Host Header blank. therefore, a port can have only one site with an empty Host header.
We have set up Asp.net ISAPI to take over the children who do not have a home. Write the program, plan the input URL, and run rewrite.

Why does my httpmodule fail to function?

After a breakpoint is set in the httpmodule program, the program does not go from there in any case. the reason is that you did not register your httpmodule program with the web program. the working requirement is on the web. config.
<System. Web>
<Httpmodules>
<Add type = "urlrerecordr. modulererecordr, urlrerecordr" Title = "modulererecordr"/>
</Httpmodules>
</System. Web>

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

This is because you did not register your rerecordrconfig configuration section with the web program. The work requirement is completed in Web. config.
<Configsections>
<Section title = "rerecordrconfig" type = "urlrerecordr. config. rerecordrconfigserializersectionhandler, urlrerecordr"/>
</Configsections>
Then, you have no score and applied the rerecordrconfig section in <configuration> to configure the rules.

Which part of the HTTP module processes the URL?

Most of them work in the urlrerecordr. modulererecordr. rerecord () tips. The key stages are:
If (Re. ismatch (requestedpath ))
Obviously, the URL passed in May be the URL we want to rewrite,
String sendtourl = rerecordrutils. resolveurl (App. Context. Request. applicationpath, re. resocate (requestedpath, rules [I]. sendto ));
The target URL configured in Web. config is accepted.
Rerecordrutils. rerecordurl (App. Context, sendtourl );
Rewrite the URL internally.

I don't want to write a 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 requirement.
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 that time, we needed to do some work in the source code mentioned above.
If (Re. ismatch (requestedpath ))
{

// Locate the Level 2 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 ";

Rerecordrutils. rerecordurl (App. Context, sendtourl );

}

Configure the rule time in Web. config.
<Rerecordrrule>
<Lookfor> http: // (/W +)/. Kerry/. com </lookfor>
<Sendto>/test. aspx </sendto>
</Rerecordrrule>
(/W +) match any char string
Test. aspx has no score for anything else, because we didn't use it at all.

I have a lot of sites that are not sure about Level 2 domain names, but the page of each site determines that the content of each level 2 domain name site is actually different from the root play ID from the database,
The situation is as follows
Http: // localhost/Kerry/action. aspx? Id = 1 love.kerry.com/developer.aspx

Http: // localhost/Kerry/action. aspx? Id = 14 like.kerry.com/developer.aspx

Currently, the ID parameter cannot be displayed, and all the methods are changed to Level 2 domain names. What should I do at that time?

Configure rules first
<Rerecordrrule>
<Lookfor> http: // (/W +)/. Kerry/. com/walk. aspx </lookfor>
<Sendto>/action. aspx </sendto>
</Rerecordrrule>
And then process it like that in the program.
// Obtain the second-level domain name
String [] Userhost = app. Request. url. Host. Split (New char [] {'.'});
String domain2 = Userhost [0];
Different numbers are obtained based on the domain name
Int id = getidfromdomain (domain2 );
// Obtain the basic URL to be switched
String sendtourl = rerecordrutils. resolveurl (App. Context. Request. applicationpath, re. resocate (requestedpath, rules [I]. sendto ));
// Add the ID Parameter
If (ID> 0)
Sendtourl = string. Format ("{0 }? Id = {1} ", sendtourl, ID );
Else
Sendtourl = "error. aspx ";
// Rewrite
Rerecordrutils. rerecordurl (App. Context, sendtourl );

I found another solution on the internet...

Maybe you are referring to this text.

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

The basic tips are the same. the reason why the column is not listed at the beginning is that it is a bit clever, and it is estimated that it is not so easy to understand at the beginning. however, I believe that the last buddy will smile at the work again.
Happy Programming

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.