I haven’t written a technical article for a long time. If you don’t understand it, just read a few more articles, or reply at the end of the article (this is the most effective way). I will try my best to help you answer your doubts.
I came here to find this article. , you should all know what a second-level domain name is, so I won’t talk nonsense. But before discussing, you must first understand an ideological issue.
The problem that many friends have been unable to figure out (I also couldn’t figure it out a few days ago) is that after I type in an address, why is the URL rewritten?
Step 1: Type an address into the browser, such as http://blog.downcodes.com , and click Enter. What happens?
In order to simplify the problem, let me explain it like this:
Step 2: First, the typed address is parsed and finally comes to a web server. It is handed over to IIS for processing. In the .net world, IIS will hand over such a request to a web processor for processing. Finally, the The web processor returns the processing results to the browser and displays them to the user.
Please don't ignore this problem. Everything in the second step is done on the server side. When these things are going on, the address on the client's browser will not change. Even if the web processor returns the processing result in the end time, the above address will not change.
The URL you type in at the beginning only acts as a knock on the door. Once the door is knocked, the function is over. Only your eyes can see the address. The browser, server, etc. do not know this address.
The problem that needs to be understood is that the so-called URL rewriting is only an inside story known to web developers. The user has no idea what is going on. He thinks that the address he types is the result that should be displayed on the screen. In other words, we Control what is displayed behind the scenes.
The next thing to consider is how to control the displayed content?
From the process mentioned above, it is obvious that the work of the web processor needs to be manipulated.
One of the simplest considerations is that the user enters a simple address without any parameters, http://blog.downcodes.com and then We change this address to an address with parameters that meets the needs of the program, http://kerry.com?lover=notus , and finally process it.
The so-called url rewriting is at this step.
In .net terms, we need to register an httpmodule for the application to handle specific URLs
Register httpmodule in web.config,
is roughly equivalent to this program
in the httpmodule program we provide
// Use our httpmodule program to intercept the original URL
String OriginalUrl=” http://blog.downcodes.com ”;
//Process the original url and get the last 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 implement url rewriting.
Step 1: Determine which URLs need to be rewritten, that is, formulate rewriting rules. Step 2: Write an httpmodule handler. Step 3: Integrate the written httpmodule into the web program and start working.
The above is the basic knowledge of url rewriting. , and using url rewriting to implement a second-level domain name, the process is the same. Because whether it is a second-level domain name or a third-level domain name, it is a URL address. As long as we intercept this URL address, we can manipulate it during processing.
These tasks are quite Troublesome, but there are experts on the Internet who have written such a program for us. Please 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.htmlThe
article is over.
In the implementation process You will encounter some problems, most of which are caused by not reading the above article carefully, but to be honest, it is not easy to read such a long article. Let me record some important questions below. The last two questions, Use specific code to show how to handle the rewritten target URL to meet our requirements
What is Microsoft's URLRewriter? Where can I download this project?
This is a sample program provided in an article on msdn introducing URLRewriter. It can be downloaded here.
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
How to use these codes? Is it troublesome?
To be sure, it’s not a hassle. Here are the things to do:
Download the code to your machine.
After installation, add the URLRewriter project to your own project, follow the method in the address given above, modify the code configuration web.config, and start using it.
What is httpmodule?
A simple understanding is that it is a program that processes http requests. For a more detailed understanding, please refer to the sdk documentation.
How to implement pan-resolution?
First, add a second-level domain name of *.kerry.com to the domain name service provider, pointing to your server IP
Then, create a site in IIS, leave the host header of this site blank, and the general port is 80. This site is the default website for the entire server port 80.
Add a wildcard application mapping to this site (IIS Site Properties->Home Directory->Configuration). The purpose of this mapping is to have asp.net ISAPI take over any secondary domain name site that is not specified in IIS.
What happens when you enter a second-level domain name?
When IIS detects that the incoming URL is a second-level domain name, it will first check whether there is a site registered with this second-level domain name on IIS. If so, it will transfer to this site, otherwise, it will transfer to the default Site, this default site is the site with a previously configured host header that is empty. Therefore, a port can only have one site with an empty host header.
We have set up asp.net ISAPI to take over these homeless children. Write a program to analyze the incoming URL and perform rewriting.
Why does my httpmodule not seem to work?
After setting a breakpoint in the httpmodule program, no matter what, the process does not go from here. The reason is that you have not registered your httpmodule program with the web program. This needs to be done in web.config Completed in.
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
</system.web>
Why am I always prompted with "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>
You can then configure the rules using the RewriterConfig section in <configuration>.
In which part of httpmodule is the url processed?
Most of the work is in the URLRewriter. ModuleRewriter. Rewrite() method. The key stage is here:
if (re.IsMatch(requestedPath))
Obviously, this determines whether the incoming url is the url we want to rewrite. Let's take a look.
String sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
The target URL configured in web.config is received here.
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
Rewrite the url internally.
I don’t want to hard-code the second-level domain name in web.config, and the target URL I want to rewrite cannot be hard-coded. For example, if we have such a need
The actual processing page of blog.downcodes.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 walkwith.kerry.com is kerry.com/walk.aspx
How to deal with it?
At this time, you need to make some manipulations in the codes 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 if(domain2==”walkwith”)
sendToUrl =”/walk.aspx”;
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
}
When configuring rules in web.config, you need to do this
<RewriterRule>
<LookFor>http://(w+).kerry.com</LookFor>
<SendTo>/test.aspx</SendTo>
</RewriterRule>
(w+) is used to match any string. You can write anything else in test.aspx here, because we don’t use it at all.
I have many sites with uncertain second-level domain names, but the pages of each site are determined. The content of each second-level domain name site is actually retrieved from the database based on different IDs.
The situation is like this
http://localhost/kerry/action.aspx?id=1 blog.downcodes.com/walk.aspx
http://localhost/kerry/action.aspx?id=14 like.kerry.com/walk.aspx
Pass now When I go up, the id parameter cannot be displayed and is changed to the second-level domain name. What should I do at this time?
First configure the rules.
<RewriterRule>
<LookFor>http://(w+).kerry .com walk.aspx</LookFor>
<SendTo>/action.aspx</SendTo>
</RewriterRule>
Then handle it like this in the program
//Get the second-level domain name
string [] UserHost = app.Request.Url.Host.Split ( new Char [] { '.' } );
string domain2=UserHost [0];
Get different numbers based on domain name
int id=getIDfromDomain(domain2);
//Get the basic url to be redirected
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
//Add id parameter
if(id>0)
sendToUrl=string.Format ( "{0}?id={1}" , sendToUrl , id );
else
sendToUrl=”error.aspx”;
//Rewrite
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
I searched online and there is another solution...
Maybe you are referring to 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 this is not listed at the top is because this approach is a bit tricky and may not be easy to understand at first. But I believe that friends who have seen it at the end Reading this article again will probably make you smile knowingly
Happy programming
Final ps: I was planning to publish this article in a few days, but just now, I accidentally deleted all the programs I wrote in the past few days from VSS. It was still a complete type. Once again, I strongly despise this garbage source code manager made by Microsoft-_ -