. NET implements any two-level domain name with URL rewrite urlrewriter

Source: Internet
Author: User
Tags server port

. NET with URL rewrite Urlrewriter implementation of any two-level domain name these two days need to use urlrewriter to make that pig's blog, online to see a good article, collection

Summary:
Explains the URLThe related knowledge of rewriting. Using ASP . NETAchieve Level Two Domain nameThe overridden method. A summary of some of the issues that have been rewritten. provides several sample code.

Long time no technical articles, if you do not understand, see a few more, sweat, or, in the back of the article (this is the most effective way), I will try to help you answer doubts.

To find this article, should know what is called Level two Domain name, it won't be said. But before the discussion, we must understand a question of thought.
A lot of friends have been thinking about (I have been confused a few days ago) The problem is that I typed an address after how this URLhave been rewritten?
The first step: Type an address in the browser, such ashttp://love.kerry.com, what happens when you click enter?
To simplify the question, let me explain this:
The second step: first, the typed address is parsed, and finally came to a Web server. Give it to IIS for processing. In . NETWorld, IIS will hand over such a request to a web processor, and finally, the Web processor returns the result of the processing to the browser, which is displayed to the user.
Please do not ignore such a problem, the second step of everything 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 above address will not change.
The first type of URL, just play a knock on the role, the door knocked over, even if the end of the effect, only your eyes can see that address, browser, server and so do not know this address.
And then the question to understand is that the so-called URLRewriting, which is just what the web developer knows, the user doesn't know what's going on, and he thinks that the address he's typing is the result that should appear on the screen. That is, we control what is displayed behind the scenes.
The next thing to consider is how to control what is displayed?
From the process described above, it is obvious to work this step in the Web processor's hands.

One of the simplest considerations is that the user has typed in a simple address without any parameters,http://love.kerry.comAnd then we change the address to a parameter that meets the needs of the program,Http://kerry.com?lover=notus, and finally deal with it.
The so-called URLRewrite, that is in this step.
Use . NETTerminology, we need to register an HttpModule with the application to handle a specific URL
Register HttpModule, in Web. config,
Processing URL, in the HttpModule program we provide

Roughly equivalent to a program like this

Use our HttpModule program to intercept the original URL
String originalurl= "http://love.kerry.com”;
Process the original URLAnd get the last needed URL, the value isHttp://kerry.com?lover=notus
String Finalurl=rewrite (Originalurl);
The context re- URLInternally sent to IIS for processing
Context. RewritePath (FinalURL);

Next, we'll implement URLRewrite.
The first step: Determine which URLPerform overrides, i.e. make rewrite rules
Step two: Write the HttpModule handler
Step three: Integrate the written httpmodule into the Web program and start working.

Above is URLOverriding the basics, and using the URLOverride implementation Level Two Domain name, the process is the same. Because either the level two Domain nameor Level three? Domain name, is a URLAddress. As long as we intercept this. URLAddress, you can handle the foot at the time of processing.

These work is very troublesome, but there are already on the network to write to us such a program, see the following article:

http://www.microsoft.com/china/m ... t/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.

In the implementation process will encounter some problems, mostly because the above article is not carefully produced, but to tell the truth, so long articles to read is not easy. Let me take a look at some important questions. One of the last two questions, with specific code showing how to handle the overridden target URLTo meet our requirements.


What is Microsoft's Urlrewriter? Where did you download the project?
This is the sample program provided in an article on MSDN that describes Urlrewriter, which can be downloaded here to
http://www.microsoft.com/china/m ... t/urlrewriting.mspx


How do you use the code?
Sure to say, no trouble, to do things there:
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?

Simple to understand, is a piece of processing HTTP request program
For a more detailed understanding, please consult the SDK documentation.


How to achieve pan-resolution?

First, in Domain nameAdd a *.kerry.com level two to the service provider Domain name, point to your server IP
Then, establish a site in IIS, the host 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 mapping to this site (configuration, home directory, IIS site properties), which is intended to be an ASP . NETISAPI takes over any two levels that are not explicitly in IIS Domain nameSite.


What happens when you enter a two-level domain name ?
When IIS detects an incoming URLis a level two Domain name, it will first check whether IIS has registered this level two Domain nameSite, if there, will be transferred to this site, otherwise, will go to the default site, the default site is the previous configuration of the main header is empty site. Therefore, a port can only have one host header empty site.
We have set up the ASP . NETISAPI takes over these children who have no home. Write the program, analyze the incoming URL, perform the override.


Why doesn't my httpmodule seem to work?

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


Why always prompt me for "Unknown configuration section rewriterconfig error"

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


what part of the HttpModule is the URL processed in?

Most of the work is done in Urlrewriter. Modulerewriter. Rewrite () method. The Key stage is here:
if (re. IsMatch (Requestedpath))
Obviously, the judgment passed in URLIf we're going to rewrite it. URL, and we went on to see,
String Sendtourl = Rewriterutils.resolveurl (app. Context.Request.ApplicationPath, Re. Replace (Requestedpath, rules . SendTo));
This takes you to the destination that you configured in Web. config to go toURL
Rewriterutils.rewriteurl (app. Context, Sendtourl);
In the interior of theURLRewrite.


I don't want to write a two-level domain name in Web. config, and the target URL i want to rewrite cannot be written to death. For example, 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 handle it?


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

Found itURLLevel two in theDomain name
string [] Userhost = App. Request.URL. Host.split (New Char [] {'. '});
String domain2=userhost [0];

Set the target to be rewritten as neededURL
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);

}

This is required when configuring rules in Web. config
<RewriterRule>
<LookFor>/http (/w+)/.kerry/.com</lookfor>
<SendTo>/test.aspx</SendTo>
</RewriterRule>
(/w+) 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 unsure about the level two domain name site, but each site's page determines, each two level domain name site's content actually the root play different ID from the database tune,
The situation is 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 pass up, can't display ID parameter, change to 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 handle it in the program.
Get level TwoDomain name
string [] Userhost = App. Request.URL. Host.split (New Char [] {'. '});
String domain2=userhost [0];
UnderDomain nameGet a different number
int Id=getidfromdomain (DOMAIN2);
Get to the basic of turningURL
String sendtourl = Rewriterutils.resolveurl (app. Context.Request.ApplicationPath, Re. Replace (Requestedpath, rules. SendTo));
Plus ID parameter
if (id>0)
Sendtourl=string. Format ("{0}?id={1}", Sendtourl, id);
Else
Sendtourl= "Error.aspx";
Rewrite
Rewriterutils.rewriteurl (app. Context, Sendtourl);

How do i match a directory? wrote a lookfor rule http://love.kerry.com/, but in the browser input this address, always can not be correctly rewritten, after trace found at all can not match, why?

First of all, we should know that the browser actually accepts nothttp://love.kerry.com/Buthttp://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 are index.aspx or other strange name, write your own name.
SameHttp://love.kerry.com/funThis address needs to be matched to this rule.http://love.kerry.com/fun/default.aspx

But, again, your file does not need to have fun this directory, because ... Rewrite it.

I found another solution to the Internet ...

Maybe you mean this article.

http://blog.csdn. NET/mengyao/archive/2007/01/25/1493537.aspx

As you can see, the basic approach is the same. This is not the first, because it is somewhat trickery, and may not be so well understood at the beginning. But I believe seeing the last friend read this article, should be a smile
Happy programming

Posted on 2007-07-08 01:29 Leon Qian Read (374) Comments (2) Edit Collection

View comments#1 Floor Reply reference I have a here about ASP . NETTo achieve level two or multistage in Domain name(modify Urlrewrite) need to pay attention to the problem, I would like to ask you
I don't want to take level two. Domain nameWrite dead in Web. config, and I want to rewrite the target URLI can't write dead.
Like what:
Http://localhost/mdcn.cn/page?name=wutai This is the full address.
Overridden path: wutai.mdcn.cn/wutai.aspx (HTML)
If Name=mymdoel changes, rewrite the level two. Domain nameAnd it turns into a mymdoel.
The rewritten virtual path: mdcn.cn/mymdoel.aspx. In other words, the name of the name is not fixed inside, I am looking at you in the. cs file changed the code to write it dead.
if (re. IsMatch (Requestedpath))
{
Found it URLLevel two in the Domain name
string [] Userhost = App. Request. URL. Host.split (New Char [] {'. '});
String domain2=userhost [0];

Set the target to be rewritten as needed URL
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);
}
I can not write dead, because the name is read from the database, the corresponding information is the name of a separate network, if you want to add the page, it will be in the. cs file to change it, and then generate the. dll file It's too much trouble, it's not good, but you can not teach me here by the parameter name, NAEM and change level two. Domain name, how to implement this function

. NET implements any two-level domain name with URL rewrite urlrewriter

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.